|
@@ -3,531 +3,533 @@ const util = require("utils/util");
|
|
|
const jsonlint = require("utils/jsonlint");
|
|
|
|
|
|
App({
|
|
|
- onLaunch: function () {
|
|
|
- // 获取系统状态栏信息
|
|
|
- wx.getSystemInfo({
|
|
|
- success: e => {
|
|
|
- this.globalData.StatusBar = e.statusBarHeight;
|
|
|
- let capsule = wx.getMenuButtonBoundingClientRect();
|
|
|
- if (capsule) {
|
|
|
- this.globalData.Custom = capsule;
|
|
|
- // this.globalData.CustomBar = capsule.bottom + capsule.top - e.statusBarHeight;
|
|
|
- // this.globalData.CustomBar = (capsule.top - e.statusBarHeight) * 2 + capsule.height + e.statusBarHeight;
|
|
|
- // this.globalData.CustomBar = capsule.height + e.statusBarHeight + capsule.top;
|
|
|
- this.globalData.CustomBar = 44 + e.statusBarHeight;
|
|
|
- } else {
|
|
|
- this.globalData.CustomBar = e.statusBarHeight + 50;
|
|
|
- }
|
|
|
- }
|
|
|
- });
|
|
|
- },
|
|
|
-
|
|
|
- /**
|
|
|
- * 全局数据
|
|
|
- */
|
|
|
- globalData: {
|
|
|
- Custom: 50,
|
|
|
- userInfo: null,
|
|
|
- code: "",
|
|
|
- isWxLogin: false,
|
|
|
- openid: "",
|
|
|
- session_key: "",
|
|
|
- token_type: "",
|
|
|
- access_token: "",
|
|
|
- username: "",
|
|
|
- password: ""
|
|
|
- },
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取域
|
|
|
- * @param {string} param 参数
|
|
|
- */
|
|
|
- GetDomain: function (param) {
|
|
|
- var domain = '';
|
|
|
- param = typeof param !== 'string' ? '' : param.toUpperCase();
|
|
|
- switch (param) {
|
|
|
- // 本地
|
|
|
- // case 'AUTHSERVER':
|
|
|
- // domain = 'http://localhost:5100';
|
|
|
- // break;
|
|
|
- // case 'HOST':
|
|
|
- // default:
|
|
|
- // domain = 'http://localhost:5101';
|
|
|
-
|
|
|
- // 程雪阳
|
|
|
- // case 'AUTHSERVER':
|
|
|
- // domain = 'http://192.168.3.154:5100';
|
|
|
- // break;
|
|
|
- // case 'HOST':
|
|
|
- // default:
|
|
|
- // domain = 'http://192.168.3.154:5101';
|
|
|
-
|
|
|
- // 测试
|
|
|
- // case 'AUTHSERVER':
|
|
|
- // domain = 'https://www.befrisoft.com:6100';
|
|
|
- // break;
|
|
|
- // case 'HOST':
|
|
|
- // default:
|
|
|
- // domain = 'https://www.befrisoft.com:6101';
|
|
|
-
|
|
|
- // 正式
|
|
|
- case 'AUTHSERVER':
|
|
|
- domain = 'https://www.sun-hoo.cn:6100';
|
|
|
- break;
|
|
|
- case 'HOST':
|
|
|
- default:
|
|
|
- domain = 'https://www.sun-hoo.cn:6101';
|
|
|
- }
|
|
|
- return domain;
|
|
|
- },
|
|
|
-
|
|
|
- /**
|
|
|
- * 授权
|
|
|
- */
|
|
|
- Authorize: function (paramenter) {
|
|
|
- wx.getSetting({
|
|
|
- success: function (res) {
|
|
|
- /**
|
|
|
- * 如果没有授权,进行授权操作
|
|
|
- * 授权成功后,调用授权完成方法
|
|
|
- * 如果已授权,调用授权完成方法
|
|
|
- */
|
|
|
- if (!res.authSetting["scope." + paramenter.scope]) {
|
|
|
- wx.authorize({
|
|
|
- scope: paramenter.scope,
|
|
|
- success: function (res) {
|
|
|
- if (typeof paramenter.success === "function") {
|
|
|
- paramenter.success(res);
|
|
|
- }
|
|
|
- },
|
|
|
- fail: function () {
|
|
|
- if (typeof paramenter.fail === "function") {
|
|
|
- paramenter.fail();
|
|
|
- }
|
|
|
- }
|
|
|
- });
|
|
|
- } else if (typeof paramenter.success === "function") {
|
|
|
- paramenter.success();
|
|
|
- }
|
|
|
- }
|
|
|
- });
|
|
|
- },
|
|
|
-
|
|
|
- /**
|
|
|
- * 请求
|
|
|
- */
|
|
|
- Request: function (options, notVerify) {
|
|
|
- var app = this;
|
|
|
- if (!(typeof options.url === 'string' && options.url.length > 0)) {
|
|
|
- return;
|
|
|
- }
|
|
|
- options.url += (options.url.indexOf('?') > 0 ? '&' : '?') + 'v=' + Date.now().toString();
|
|
|
- options.header = typeof options.header === 'object' ? options.header : {};
|
|
|
- options.method = options.method || 'get';
|
|
|
- options.header['content-type'] = util.stringIsNullOrWhiteSpace(options.header['content-type']) ? 'application/json' : options.header['content-type'];
|
|
|
- switch (options.header['content-type']) {
|
|
|
- case 'application/json':
|
|
|
- switch (options.method) {
|
|
|
- case 'post':
|
|
|
- case 'put':
|
|
|
- case 'delete':
|
|
|
- if (typeof options.data === 'object') {
|
|
|
- options.data = JSON.stringify(options.data);
|
|
|
- }
|
|
|
- break;
|
|
|
- default:
|
|
|
- if (typeof options.data === 'object') {
|
|
|
- options.data = util.encodeObject(options.data);
|
|
|
- }
|
|
|
- }
|
|
|
- break;
|
|
|
- }
|
|
|
- if (notVerify) {
|
|
|
- Send();
|
|
|
- } else {
|
|
|
- app.GetAuth({
|
|
|
- success: res => {
|
|
|
- options.header.Authorization = res.token_type + ' ' + res.access_token;
|
|
|
- Send();
|
|
|
- },
|
|
|
- fail: res => {
|
|
|
- options.fail(res);
|
|
|
- }
|
|
|
- });
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 发送请求方法
|
|
|
- */
|
|
|
- function Send() {
|
|
|
- wx.request({
|
|
|
- url: options.url || "",
|
|
|
- method: options.method || "get",
|
|
|
- header: options.header || null,
|
|
|
- data: options.data || {},
|
|
|
- dataType: options.dataType || 'text',
|
|
|
- enableCache: false,
|
|
|
- success: res => {
|
|
|
- try {
|
|
|
- if (res.statusCode === 200) {
|
|
|
- var result = {};
|
|
|
- if (!util.stringIsNullOrWhiteSpace(res.data)) {
|
|
|
- if (options.jsonlint) {
|
|
|
- result = jsonlint.parse(res.data);
|
|
|
- } else {
|
|
|
- result = JSON.parse(res.data);
|
|
|
- }
|
|
|
- } else {
|
|
|
- result = res.data;
|
|
|
- }
|
|
|
- options.success(result);
|
|
|
- } else {
|
|
|
- options.fail(util.formatError(res));
|
|
|
- }
|
|
|
- } catch (ex) {
|
|
|
- wx.showModal({
|
|
|
- title: '提示',
|
|
|
- content: util.formatError(ex).errMsg
|
|
|
- });
|
|
|
- }
|
|
|
- },
|
|
|
- fail: res => {
|
|
|
- try {
|
|
|
- options.fail(util.formatError(res));
|
|
|
- } catch (ex) {
|
|
|
- wx.showModal({
|
|
|
- title: '提示',
|
|
|
- content: util.formatError(ex).errMsg
|
|
|
- });
|
|
|
- }
|
|
|
- }
|
|
|
- });
|
|
|
- }
|
|
|
- },
|
|
|
-
|
|
|
- /**
|
|
|
- * 登录
|
|
|
- */
|
|
|
- Login: function (options) {
|
|
|
- var app = this;
|
|
|
- if (util.objectIsNullOrEmpty(options) || util.objectIsNullOrEmpty(options.data)) {
|
|
|
- return options.fail({
|
|
|
- errMsg: '参数不正确'
|
|
|
- });
|
|
|
- }
|
|
|
- if (util.stringIsNullOrWhiteSpace(options.data.username)) {
|
|
|
- return options.fail({
|
|
|
- errMsg: '账号为空'
|
|
|
- });
|
|
|
- }
|
|
|
- if (util.stringIsNullOrWhiteSpace(options.data.password)) {
|
|
|
- return options.fail({
|
|
|
- errMsg: '密码为空'
|
|
|
- });
|
|
|
- }
|
|
|
- options.data.grant_type = 'password';
|
|
|
- options.data.client_id = 'sunhooDeskApp';
|
|
|
- //options.data.client_id = 'SunhooWeb';
|
|
|
- options.data.client_secret = 'sunhoo';
|
|
|
- //options.data.client_secret = 'Sunhoo';
|
|
|
- options.header = typeof options.header === 'object' ? options.header : {};
|
|
|
- options.header['content-type'] = 'application/x-www-form-urlencoded';
|
|
|
- this.Request({
|
|
|
- url: app.GetDomain('authserver') + '/connect/token',
|
|
|
- method: options.type || 'post',
|
|
|
- header: options.header,
|
|
|
- data: options.data,
|
|
|
- dataType: options.dataType,
|
|
|
- success: res => {
|
|
|
- app.SetAuth(res);
|
|
|
- // 获取用户信息
|
|
|
- app.GetCurrentUser({
|
|
|
- success: result => {
|
|
|
- app.SetAccount({
|
|
|
- username: options.data.username,
|
|
|
- password: options.data.password
|
|
|
- });
|
|
|
- app.SetUser(result);
|
|
|
- options.success(res);
|
|
|
- },
|
|
|
- fail: result => {
|
|
|
- options.fail(result);
|
|
|
- }
|
|
|
- });
|
|
|
- },
|
|
|
- fail: res => {
|
|
|
- options.fail(res);
|
|
|
- }
|
|
|
- }, true);
|
|
|
- },
|
|
|
-
|
|
|
- /**
|
|
|
- * 重新登录
|
|
|
- */
|
|
|
- Relogin: function (options) {
|
|
|
- var app = this;
|
|
|
- this.GetAccount({
|
|
|
- success: function (res) {
|
|
|
- app.Login({
|
|
|
- data: res,
|
|
|
- success: function (res) {
|
|
|
- app.SetAuth(res);
|
|
|
- if (typeof options.success === 'function') {
|
|
|
- options.success(res);
|
|
|
- }
|
|
|
- },
|
|
|
- fail: function (res) {
|
|
|
- app.RemoveAuth();
|
|
|
- options.fail(res);
|
|
|
- }
|
|
|
- });
|
|
|
- },
|
|
|
- fail: res => {
|
|
|
- options.fail(res);
|
|
|
- }
|
|
|
- });
|
|
|
- },
|
|
|
-
|
|
|
- /**
|
|
|
- * 登出
|
|
|
- */
|
|
|
- Logout: function (options) {
|
|
|
- try {
|
|
|
- this.RemoveAuth();
|
|
|
- this.RemoveAccount();
|
|
|
- this.RemoveUser();
|
|
|
- if (typeof options.success === 'function') {
|
|
|
- options.success();
|
|
|
- }
|
|
|
- } catch (ex) {
|
|
|
- if (typeof options.fail === 'function') {
|
|
|
- options.fail(util.formatError(res));
|
|
|
- }
|
|
|
- }
|
|
|
- },
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取当前用户信息
|
|
|
- */
|
|
|
- GetCurrentUser: function (options) {
|
|
|
- var app = this;
|
|
|
- this.Request({
|
|
|
- jsonlint: true,
|
|
|
- url: this.GetDomain() + '/api/BefriUser/CurrentUser',
|
|
|
- success: res => {
|
|
|
- app.SetUser(res);
|
|
|
- options.success(res);
|
|
|
- },
|
|
|
- fail: res => {
|
|
|
- options.fail(res);
|
|
|
- }
|
|
|
- });
|
|
|
- },
|
|
|
-
|
|
|
- /**
|
|
|
- * 设置认证信息
|
|
|
- */
|
|
|
- SetAuth: function (data) {
|
|
|
- wx.setStorage({
|
|
|
- key: 'auth',
|
|
|
- data: data || '',
|
|
|
- });
|
|
|
- },
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取认证信息
|
|
|
- */
|
|
|
- GetAuth: function (data) {
|
|
|
- wx.getStorage({
|
|
|
- key: 'auth',
|
|
|
- success: res => {
|
|
|
- data.success(res.data);
|
|
|
- },
|
|
|
- fail: res => {
|
|
|
- data.fail(util.formatError(res));
|
|
|
- }
|
|
|
- });
|
|
|
- },
|
|
|
-
|
|
|
- /**
|
|
|
- * 删除认证信息
|
|
|
- */
|
|
|
- RemoveAuth: function () {
|
|
|
- wx.removeStorage({
|
|
|
- key: 'auth'
|
|
|
- });
|
|
|
- },
|
|
|
-
|
|
|
- /**
|
|
|
- * 判断认证是否过期
|
|
|
- * */
|
|
|
- AuthIsExpires: function (data) {
|
|
|
- this.GetAuth({
|
|
|
- success: function (res) {
|
|
|
- try {
|
|
|
- if (typeof res === 'object' && res.date > new Date()) {
|
|
|
- data.success();
|
|
|
- } else {
|
|
|
- data.fail();
|
|
|
- }
|
|
|
- } catch (ex) {
|
|
|
- data.fail(ex);
|
|
|
- }
|
|
|
- }
|
|
|
- });
|
|
|
- },
|
|
|
-
|
|
|
- /**
|
|
|
- * 设置账户信息
|
|
|
- */
|
|
|
- SetAccount: function (data) {
|
|
|
- wx.setStorage({
|
|
|
- key: 'account',
|
|
|
- data: data || '',
|
|
|
- });
|
|
|
- },
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取账户信息
|
|
|
- */
|
|
|
- GetAccount: function (data) {
|
|
|
- wx.getStorage({
|
|
|
- key: 'account',
|
|
|
- success: res => {
|
|
|
- data.success(res.data);
|
|
|
- },
|
|
|
- fail: res => {
|
|
|
- data.fail(util.formatError(res));
|
|
|
- }
|
|
|
- })
|
|
|
- },
|
|
|
-
|
|
|
- /**
|
|
|
- * 删除账户信息
|
|
|
- */
|
|
|
- RemoveAccount: function () {
|
|
|
- wx.removeStorage({
|
|
|
- key: 'account'
|
|
|
- });
|
|
|
- },
|
|
|
-
|
|
|
- /**
|
|
|
- * 设置用户信息
|
|
|
- */
|
|
|
- SetUser: function (data) {
|
|
|
- wx.setStorage({
|
|
|
- key: 'user',
|
|
|
- data: data || '',
|
|
|
- });
|
|
|
- },
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取用户信息
|
|
|
- */
|
|
|
- GetUser: function (data) {
|
|
|
- wx.getStorage({
|
|
|
- key: 'user',
|
|
|
- success: res => {
|
|
|
- data.success(res.data);
|
|
|
- },
|
|
|
- fail: res => {
|
|
|
- data.fail(util.formatError(res));
|
|
|
- }
|
|
|
- })
|
|
|
- },
|
|
|
-
|
|
|
- /**
|
|
|
- * 删除用户信息
|
|
|
- */
|
|
|
- RemoveUser: function () {
|
|
|
- wx.removeStorage({
|
|
|
- key: 'user'
|
|
|
- });
|
|
|
- },
|
|
|
-
|
|
|
- /**
|
|
|
- * 验证
|
|
|
- */
|
|
|
- Validate: {
|
|
|
- //验证邮箱
|
|
|
- Email: function (value) {
|
|
|
- return /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/.test(value);
|
|
|
- },
|
|
|
-
|
|
|
- //验证 url 地址
|
|
|
- Url: function (value) {
|
|
|
- return /^(?:(?:(?:https?|ftp):)?\/\/)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})).?)(?::\d{2,5})?(?:[/?#]\S*)?$/i.test(value);
|
|
|
- },
|
|
|
-
|
|
|
- //验证日期
|
|
|
- Date: function (value) {
|
|
|
- return !/Invalid|NaN/.test(new Date(value).toString());
|
|
|
- },
|
|
|
-
|
|
|
- //验证 ISO 格式日期
|
|
|
- DateISO: function (value) {
|
|
|
- return /^\d{4}[\/\-](0?[1-9]|1[012])[\/\-](0?[1-9]|[12][0-9]|3[01])$/.test(value);
|
|
|
- },
|
|
|
-
|
|
|
- //验证数字
|
|
|
- Number: function (value) {
|
|
|
- return /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/.test(value);
|
|
|
- },
|
|
|
-
|
|
|
- //验证整数
|
|
|
- Digits: function (value) {
|
|
|
- return /^\d+$/.test(value);
|
|
|
- },
|
|
|
-
|
|
|
- //比较最小长度
|
|
|
- Minlength: function (value, param) {
|
|
|
- var length = value instanceof Array ? value.length : typeof value == 'string' ? value.length : 0;
|
|
|
- return length >= param;
|
|
|
- },
|
|
|
-
|
|
|
- //比较最大长度
|
|
|
- Maxlength: function (value, param) {
|
|
|
- var length = value instanceof Array ? value.length : typeof value == 'string' ? value.length : 0;
|
|
|
- return length <= param;
|
|
|
- },
|
|
|
-
|
|
|
- //验证长度区间
|
|
|
- Rangelength: function (value, param) {
|
|
|
- var length = value instanceof Array ? value.length : typeof value == 'string' ? value.length : 0;
|
|
|
- return length >= param[0] && length <= param[1];
|
|
|
- },
|
|
|
-
|
|
|
- //比较最小值
|
|
|
- Min: function (value, param) {
|
|
|
- return value >= param;
|
|
|
- },
|
|
|
-
|
|
|
- //比较最大值
|
|
|
- Max: function (value, param) {
|
|
|
- return value <= param;
|
|
|
- },
|
|
|
-
|
|
|
- //验证数字区间
|
|
|
- Range: function (value, param) {
|
|
|
- return value >= param[0] && value <= param[1];
|
|
|
- },
|
|
|
-
|
|
|
- // 验证手机号
|
|
|
- PhoneNumber: function (value) {
|
|
|
- return /^1(3|4|5|7|8|9)\d{9}$/.test(value);
|
|
|
- },
|
|
|
-
|
|
|
- // 验证身份证
|
|
|
- IDCard: function (value) {
|
|
|
- return /^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{4}$/.test(value);
|
|
|
- },
|
|
|
-
|
|
|
- // 验证密码
|
|
|
- Password: function (value, min, max) {
|
|
|
- min = Number.IsNullOrEmpty(min) ? 1 : min;
|
|
|
- max = Number.IsNullOrEmpty(max) ? 256 : max;
|
|
|
- var regexp = eval('/^([a-z]|[A-Z]|[0-9]|[_-]){' + min + ',' + max + '}$/');
|
|
|
- return regexp.test(value);
|
|
|
- }
|
|
|
- }
|
|
|
+ onLaunch: function () {
|
|
|
+ // 获取系统状态栏信息
|
|
|
+ wx.getSystemInfo({
|
|
|
+ success: e => {
|
|
|
+ this.globalData.StatusBar = e.statusBarHeight;
|
|
|
+ let capsule = wx.getMenuButtonBoundingClientRect();
|
|
|
+ if (capsule) {
|
|
|
+ this.globalData.Custom = capsule;
|
|
|
+ // this.globalData.CustomBar = capsule.bottom + capsule.top - e.statusBarHeight;
|
|
|
+ // this.globalData.CustomBar = (capsule.top - e.statusBarHeight) * 2 + capsule.height + e.statusBarHeight;
|
|
|
+ // this.globalData.CustomBar = capsule.height + e.statusBarHeight + capsule.top;
|
|
|
+ this.globalData.CustomBar = 44 + e.statusBarHeight;
|
|
|
+ } else {
|
|
|
+ this.globalData.CustomBar = e.statusBarHeight + 50;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ },
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 全局数据
|
|
|
+ */
|
|
|
+ globalData: {
|
|
|
+ Custom: 50,
|
|
|
+ userInfo: null,
|
|
|
+ code: "",
|
|
|
+ isLogin: false,
|
|
|
+ isWxLogin: false,
|
|
|
+ openid: "",
|
|
|
+ session_key: "",
|
|
|
+ token_type: "",
|
|
|
+ access_token: "",
|
|
|
+ username: "",
|
|
|
+ password: ""
|
|
|
+ },
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取域
|
|
|
+ * @param {string} param 参数
|
|
|
+ */
|
|
|
+ GetDomain: function (param) {
|
|
|
+ var domain = '';
|
|
|
+ param = typeof param !== 'string' ? '' : param.toUpperCase();
|
|
|
+ switch (param) {
|
|
|
+ // 本地
|
|
|
+ // case 'AUTHSERVER':
|
|
|
+ // domain = 'http://localhost:5100';
|
|
|
+ // break;
|
|
|
+ // case 'HOST':
|
|
|
+ // default:
|
|
|
+ // domain = 'http://localhost:5101';
|
|
|
+
|
|
|
+ // 程雪阳
|
|
|
+ // case 'AUTHSERVER':
|
|
|
+ // domain = 'http://192.168.3.154:5100';
|
|
|
+ // break;
|
|
|
+ // case 'HOST':
|
|
|
+ // default:
|
|
|
+ // domain = 'http://192.168.3.154:5101';
|
|
|
+
|
|
|
+ // 测试
|
|
|
+ // case 'AUTHSERVER':
|
|
|
+ // domain = 'https://www.befrisoft.com:6100';
|
|
|
+ // break;
|
|
|
+ // case 'HOST':
|
|
|
+ // default:
|
|
|
+ // domain = 'https://www.befrisoft.com:6101';
|
|
|
+
|
|
|
+ // 正式
|
|
|
+ case 'AUTHSERVER':
|
|
|
+ domain = 'https://www.sun-hoo.cn:6100';
|
|
|
+ break;
|
|
|
+ case 'HOST':
|
|
|
+ default:
|
|
|
+ domain = 'https://www.sun-hoo.cn:6101';
|
|
|
+ }
|
|
|
+ return domain;
|
|
|
+ },
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 授权
|
|
|
+ */
|
|
|
+ Authorize: function (paramenter) {
|
|
|
+ wx.getSetting({
|
|
|
+ success: function (res) {
|
|
|
+ /**
|
|
|
+ * 如果没有授权,进行授权操作
|
|
|
+ * 授权成功后,调用授权完成方法
|
|
|
+ * 如果已授权,调用授权完成方法
|
|
|
+ */
|
|
|
+ if (!res.authSetting["scope." + paramenter.scope]) {
|
|
|
+ wx.authorize({
|
|
|
+ scope: paramenter.scope,
|
|
|
+ success: function (res) {
|
|
|
+ if (typeof paramenter.success === "function") {
|
|
|
+ paramenter.success(res);
|
|
|
+ }
|
|
|
+ },
|
|
|
+ fail: function () {
|
|
|
+ if (typeof paramenter.fail === "function") {
|
|
|
+ paramenter.fail();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ } else if (typeof paramenter.success === "function") {
|
|
|
+ paramenter.success();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ },
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 请求
|
|
|
+ */
|
|
|
+ Request: function (options, notVerify) {
|
|
|
+ var app = this;
|
|
|
+ if (!(typeof options.url === 'string' && options.url.length > 0)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ options.url += (options.url.indexOf('?') > 0 ? '&' : '?') + 'v=' + Date.now().toString();
|
|
|
+ options.header = typeof options.header === 'object' ? options.header : {};
|
|
|
+ options.method = options.method || 'get';
|
|
|
+ options.header['content-type'] = util.stringIsNullOrWhiteSpace(options.header['content-type']) ? 'application/json' : options.header['content-type'];
|
|
|
+ switch (options.header['content-type']) {
|
|
|
+ case 'application/json':
|
|
|
+ switch (options.method) {
|
|
|
+ case 'post':
|
|
|
+ case 'put':
|
|
|
+ case 'delete':
|
|
|
+ if (typeof options.data === 'object') {
|
|
|
+ options.data = JSON.stringify(options.data);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ if (typeof options.data === 'object') {
|
|
|
+ options.data = util.encodeObject(options.data);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ if (notVerify) {
|
|
|
+ Send();
|
|
|
+ } else {
|
|
|
+ app.GetAuth({
|
|
|
+ success: res => {
|
|
|
+ options.header.Authorization = res.token_type + ' ' + res.access_token;
|
|
|
+ Send();
|
|
|
+ },
|
|
|
+ fail: res => {
|
|
|
+ options.fail(res);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发送请求方法
|
|
|
+ */
|
|
|
+ function Send() {
|
|
|
+ wx.request({
|
|
|
+ url: options.url || "",
|
|
|
+ method: options.method || "get",
|
|
|
+ header: options.header || null,
|
|
|
+ data: options.data || {},
|
|
|
+ dataType: options.dataType || 'text',
|
|
|
+ enableCache: false,
|
|
|
+ success: res => {
|
|
|
+ try {
|
|
|
+ if (res.statusCode === 200) {
|
|
|
+ var result = {};
|
|
|
+ if (!util.stringIsNullOrWhiteSpace(res.data)) {
|
|
|
+ if (options.jsonlint) {
|
|
|
+ result = jsonlint.parse(res.data);
|
|
|
+ } else {
|
|
|
+ result = JSON.parse(res.data);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ result = res.data;
|
|
|
+ }
|
|
|
+ options.success(result);
|
|
|
+ } else {
|
|
|
+ options.fail(util.formatError(res));
|
|
|
+ }
|
|
|
+ } catch (ex) {
|
|
|
+ wx.showModal({
|
|
|
+ title: '提示',
|
|
|
+ content: util.formatError(ex).errMsg
|
|
|
+ });
|
|
|
+ }
|
|
|
+ },
|
|
|
+ fail: res => {
|
|
|
+ try {
|
|
|
+ options.fail(util.formatError(res));
|
|
|
+ } catch (ex) {
|
|
|
+ wx.showModal({
|
|
|
+ title: '提示',
|
|
|
+ content: util.formatError(ex).errMsg
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ },
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 登录
|
|
|
+ */
|
|
|
+ Login: function (options) {
|
|
|
+ var app = this;
|
|
|
+ if (util.objectIsNullOrEmpty(options) || util.objectIsNullOrEmpty(options.data)) {
|
|
|
+ return options.fail({
|
|
|
+ errMsg: '参数不正确'
|
|
|
+ });
|
|
|
+ }
|
|
|
+ if (util.stringIsNullOrWhiteSpace(options.data.username)) {
|
|
|
+ return options.fail({
|
|
|
+ errMsg: '账号为空'
|
|
|
+ });
|
|
|
+ }
|
|
|
+ if (util.stringIsNullOrWhiteSpace(options.data.password)) {
|
|
|
+ return options.fail({
|
|
|
+ errMsg: '密码为空'
|
|
|
+ });
|
|
|
+ }
|
|
|
+ options.data.grant_type = 'password';
|
|
|
+ options.data.client_id = 'sunhooDeskApp';
|
|
|
+ //options.data.client_id = 'SunhooWeb';
|
|
|
+ options.data.client_secret = 'sunhoo';
|
|
|
+ //options.data.client_secret = 'Sunhoo';
|
|
|
+ options.header = typeof options.header === 'object' ? options.header : {};
|
|
|
+ options.header['content-type'] = 'application/x-www-form-urlencoded';
|
|
|
+ this.Request({
|
|
|
+ url: app.GetDomain('authserver') + '/connect/token',
|
|
|
+ method: options.type || 'post',
|
|
|
+ header: options.header,
|
|
|
+ data: options.data,
|
|
|
+ dataType: options.dataType,
|
|
|
+ success: res => {
|
|
|
+ app.SetAuth(res);
|
|
|
+ // 获取用户信息
|
|
|
+ app.GetCurrentUser({
|
|
|
+ success: result => {
|
|
|
+ app.SetAccount({
|
|
|
+ username: options.data.username,
|
|
|
+ password: options.data.password
|
|
|
+ });
|
|
|
+ app.SetUser(result);
|
|
|
+ app.globalData.isLogin = true;
|
|
|
+ options.success(res);
|
|
|
+ },
|
|
|
+ fail: result => {
|
|
|
+ options.fail(result);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ },
|
|
|
+ fail: res => {
|
|
|
+ options.fail(res);
|
|
|
+ }
|
|
|
+ }, true);
|
|
|
+ },
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 重新登录
|
|
|
+ */
|
|
|
+ Relogin: function (options) {
|
|
|
+ var app = this;
|
|
|
+ this.GetAccount({
|
|
|
+ success: function (res) {
|
|
|
+ app.Login({
|
|
|
+ data: res,
|
|
|
+ success: function (res) {
|
|
|
+ app.SetAuth(res);
|
|
|
+ if (typeof options.success === 'function') {
|
|
|
+ options.success(res);
|
|
|
+ }
|
|
|
+ },
|
|
|
+ fail: function (res) {
|
|
|
+ app.RemoveAuth();
|
|
|
+ options.fail(res);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ },
|
|
|
+ fail: res => {
|
|
|
+ options.fail(res);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ },
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 登出
|
|
|
+ */
|
|
|
+ Logout: function (options) {
|
|
|
+ try {
|
|
|
+ this.RemoveAuth();
|
|
|
+ this.RemoveAccount();
|
|
|
+ this.RemoveUser();
|
|
|
+ if (typeof options.success === 'function') {
|
|
|
+ options.success();
|
|
|
+ }
|
|
|
+ } catch (ex) {
|
|
|
+ if (typeof options.fail === 'function') {
|
|
|
+ options.fail(util.formatError(res));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ },
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取当前用户信息
|
|
|
+ */
|
|
|
+ GetCurrentUser: function (options) {
|
|
|
+ var app = this;
|
|
|
+ this.Request({
|
|
|
+ jsonlint: true,
|
|
|
+ url: this.GetDomain() + '/api/BefriUser/CurrentUser',
|
|
|
+ success: res => {
|
|
|
+ app.SetUser(res);
|
|
|
+ options.success(res);
|
|
|
+ },
|
|
|
+ fail: res => {
|
|
|
+ options.fail(res);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ },
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设置认证信息
|
|
|
+ */
|
|
|
+ SetAuth: function (data) {
|
|
|
+ wx.setStorage({
|
|
|
+ key: 'auth',
|
|
|
+ data: data || '',
|
|
|
+ });
|
|
|
+ },
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取认证信息
|
|
|
+ */
|
|
|
+ GetAuth: function (data) {
|
|
|
+ wx.getStorage({
|
|
|
+ key: 'auth',
|
|
|
+ success: res => {
|
|
|
+ data.success(res.data);
|
|
|
+ },
|
|
|
+ fail: res => {
|
|
|
+ data.fail(util.formatError(res));
|
|
|
+ }
|
|
|
+ });
|
|
|
+ },
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除认证信息
|
|
|
+ */
|
|
|
+ RemoveAuth: function () {
|
|
|
+ wx.removeStorage({
|
|
|
+ key: 'auth'
|
|
|
+ });
|
|
|
+ },
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 判断认证是否过期
|
|
|
+ * */
|
|
|
+ AuthIsExpires: function (data) {
|
|
|
+ this.GetAuth({
|
|
|
+ success: function (res) {
|
|
|
+ try {
|
|
|
+ if (typeof res === 'object' && res.date > new Date()) {
|
|
|
+ data.success();
|
|
|
+ } else {
|
|
|
+ data.fail();
|
|
|
+ }
|
|
|
+ } catch (ex) {
|
|
|
+ data.fail(ex);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ },
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设置账户信息
|
|
|
+ */
|
|
|
+ SetAccount: function (data) {
|
|
|
+ wx.setStorage({
|
|
|
+ key: 'account',
|
|
|
+ data: data || '',
|
|
|
+ });
|
|
|
+ },
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取账户信息
|
|
|
+ */
|
|
|
+ GetAccount: function (data) {
|
|
|
+ wx.getStorage({
|
|
|
+ key: 'account',
|
|
|
+ success: res => {
|
|
|
+ data.success(res.data);
|
|
|
+ },
|
|
|
+ fail: res => {
|
|
|
+ data.fail(util.formatError(res));
|
|
|
+ }
|
|
|
+ })
|
|
|
+ },
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除账户信息
|
|
|
+ */
|
|
|
+ RemoveAccount: function () {
|
|
|
+ wx.removeStorage({
|
|
|
+ key: 'account'
|
|
|
+ });
|
|
|
+ },
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设置用户信息
|
|
|
+ */
|
|
|
+ SetUser: function (data) {
|
|
|
+ wx.setStorage({
|
|
|
+ key: 'user',
|
|
|
+ data: data || '',
|
|
|
+ });
|
|
|
+ },
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取用户信息
|
|
|
+ */
|
|
|
+ GetUser: function (data) {
|
|
|
+ wx.getStorage({
|
|
|
+ key: 'user',
|
|
|
+ success: res => {
|
|
|
+ data.success(res.data);
|
|
|
+ },
|
|
|
+ fail: res => {
|
|
|
+ data.fail(util.formatError(res));
|
|
|
+ }
|
|
|
+ })
|
|
|
+ },
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除用户信息
|
|
|
+ */
|
|
|
+ RemoveUser: function () {
|
|
|
+ wx.removeStorage({
|
|
|
+ key: 'user'
|
|
|
+ });
|
|
|
+ },
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 验证
|
|
|
+ */
|
|
|
+ Validate: {
|
|
|
+ //验证邮箱
|
|
|
+ Email: function (value) {
|
|
|
+ return /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/.test(value);
|
|
|
+ },
|
|
|
+
|
|
|
+ //验证 url 地址
|
|
|
+ Url: function (value) {
|
|
|
+ return /^(?:(?:(?:https?|ftp):)?\/\/)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})).?)(?::\d{2,5})?(?:[/?#]\S*)?$/i.test(value);
|
|
|
+ },
|
|
|
+
|
|
|
+ //验证日期
|
|
|
+ Date: function (value) {
|
|
|
+ return !/Invalid|NaN/.test(new Date(value).toString());
|
|
|
+ },
|
|
|
+
|
|
|
+ //验证 ISO 格式日期
|
|
|
+ DateISO: function (value) {
|
|
|
+ return /^\d{4}[\/\-](0?[1-9]|1[012])[\/\-](0?[1-9]|[12][0-9]|3[01])$/.test(value);
|
|
|
+ },
|
|
|
+
|
|
|
+ //验证数字
|
|
|
+ Number: function (value) {
|
|
|
+ return /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/.test(value);
|
|
|
+ },
|
|
|
+
|
|
|
+ //验证整数
|
|
|
+ Digits: function (value) {
|
|
|
+ return /^\d+$/.test(value);
|
|
|
+ },
|
|
|
+
|
|
|
+ //比较最小长度
|
|
|
+ Minlength: function (value, param) {
|
|
|
+ var length = value instanceof Array ? value.length : typeof value == 'string' ? value.length : 0;
|
|
|
+ return length >= param;
|
|
|
+ },
|
|
|
+
|
|
|
+ //比较最大长度
|
|
|
+ Maxlength: function (value, param) {
|
|
|
+ var length = value instanceof Array ? value.length : typeof value == 'string' ? value.length : 0;
|
|
|
+ return length <= param;
|
|
|
+ },
|
|
|
+
|
|
|
+ //验证长度区间
|
|
|
+ Rangelength: function (value, param) {
|
|
|
+ var length = value instanceof Array ? value.length : typeof value == 'string' ? value.length : 0;
|
|
|
+ return length >= param[0] && length <= param[1];
|
|
|
+ },
|
|
|
+
|
|
|
+ //比较最小值
|
|
|
+ Min: function (value, param) {
|
|
|
+ return value >= param;
|
|
|
+ },
|
|
|
+
|
|
|
+ //比较最大值
|
|
|
+ Max: function (value, param) {
|
|
|
+ return value <= param;
|
|
|
+ },
|
|
|
+
|
|
|
+ //验证数字区间
|
|
|
+ Range: function (value, param) {
|
|
|
+ return value >= param[0] && value <= param[1];
|
|
|
+ },
|
|
|
+
|
|
|
+ // 验证手机号
|
|
|
+ PhoneNumber: function (value) {
|
|
|
+ return /^1(3|4|5|7|8|9)\d{9}$/.test(value);
|
|
|
+ },
|
|
|
+
|
|
|
+ // 验证身份证
|
|
|
+ IDCard: function (value) {
|
|
|
+ return /^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{4}$/.test(value);
|
|
|
+ },
|
|
|
+
|
|
|
+ // 验证密码
|
|
|
+ Password: function (value, min, max) {
|
|
|
+ min = Number.IsNullOrEmpty(min) ? 1 : min;
|
|
|
+ max = Number.IsNullOrEmpty(max) ? 256 : max;
|
|
|
+ var regexp = eval('/^([a-z]|[A-Z]|[0-9]|[_-]){' + min + ',' + max + '}$/');
|
|
|
+ return regexp.test(value);
|
|
|
+ }
|
|
|
+ }
|
|
|
});
|