app.js 22.3 KB
Newer Older
D
devil_gong 已提交
1 2
App({
  data: {
D
devil 已提交
3 4 5
    // uuid缓存key
    cache_user_uuid_key: "cache_user_uuid_key",

D
devil 已提交
6 7 8
    // 配置信息缓存key
    cache_config_info_key: "cache_config_info_key",

D
devil_gong 已提交
9 10
    // 用户登录缓存key
    cache_user_login_key: "cache_user_login_key",
D
devil 已提交
11

D
devil_gong 已提交
12 13 14 15 16 17 18 19 20 21 22 23
    // 用户信息缓存key
    cache_user_info_key: "cache_shop_user_info_key",

    // 用户站点信息缓存key
    cache_user_merchant_key: "cache_shop_user_merchant_key",

    // 设备信息缓存key
    cache_system_info_key: "cache_shop_system_info_key",

    // 用户地址选择缓存key
    cache_buy_user_address_select_key: "cache_buy_user_address_select_key",

24
    // 启动参数缓存key
D
devil_gong 已提交
25 26 27 28 29 30 31 32 33 34 35
    cache_launch_info_key: "cache_shop_launch_info_key",

    // 默认用户头像
    default_user_head_src: "/images/default-user.png",

    // 成功圆形提示图片
    default_round_success_icon: "/images/default-round-success-icon.png",

    // 错误圆形提示图片
    default_round_error_icon: "/images/default-round-error-icon.png",

G
gongfuxiang 已提交
36 37
    // tabbar页面
    tabbar_pages: [
D
devil 已提交
38 39 40 41
      "/pages/index/index",
      "/pages/goods-category/goods-category",
      "/pages/cart/cart",
      "/pages/user/user",
G
gongfuxiang 已提交
42
    ],
D
devil_gong 已提交
43 44 45 46 47 48

    // 页面标题
    common_pages_title: {
      "goods_search": "商品搜索",
      "goods_detail": "商品详情",
      "user_address": "我的地址",
49
      "user_address_save_add": "添加地址",
D
devil_gong 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63
      "user_address_save_edit": "编辑地址",
      "buy": "订单确认",
      "user_order": "我的订单",
      "user_order_detail": "订单详情",
      "user_favor": "我的收藏",
      "answer_form": "留言",
      "answer_list": "问答",
      "user_answer_list": "我的留言",
      "user": "用户中心",
      "goods_category": "分类",
      "cart": "购物车",
      "message": "消息",
      "user_integral": "我的积分",
      "user_goods_browse": "我的足迹",
D
devil_gong 已提交
64
      "goods_comment": "商品评论",
D
Devil 已提交
65
      "user_orderaftersale": "退款/售后",
D
Devil 已提交
66
      "user_orderaftersale_detail": "订单售后",
D
devil_gong 已提交
67
      "user_order_comments": "订单评论",
D
devil_gong 已提交
68
      "extraction_address": "自提地址",
D
devil_gong 已提交
69 70 71
    },

    // 请求地址
G
gongfuxiang 已提交
72
    request_url: "{{request_url}}",
D
devil 已提交
73
     request_url: 'http://shopxo.com/',
D
devil 已提交
74
    // request_url: 'https://dev.shopxo.net/',
D
devil_gong 已提交
75 76 77 78

    // 基础信息
    application_title: "{{application_title}}",
    application_describe: "{{application_describe}}",
D
devil 已提交
79

D
devil 已提交
80 81
    // 货币价格符号
    currency_symbol: "{{currency_symbol}}"
D
devil_gong 已提交
82 83 84 85 86
  },

  /**
   * 小程序初始化
   */
D
devil 已提交
87
  onLaunch(params) {
88
    // 启动参数处理
D
devil 已提交
89
    params = this.launch_params_handle(params);
90

D
devil_gong 已提交
91 92 93
    // 设置设备信息
    this.set_system_info();

D
devil 已提交
94 95
    // 初始化配置
    this.init_config();
D
devil_gong 已提交
96 97 98
  },

  /**
99
   * 启动参数处理
D
devil_gong 已提交
100
   */
101 102 103 104
  launch_params_handle(params) {
    // 启动参数处理
    if ((params.query || null) != null) {
      params = params.query;
D
devil_gong 已提交
105
    }
106 107
    if ((params.scene || null) != null) {
      params = this.url_params_to_json(decodeURIComponent(params.scene));
D
devil_gong 已提交
108
    }
D
devil 已提交
109 110 111 112 113 114 115

    // 缓存启动参数
    wx.setStorage({
      key: this.data.cache_launch_info_key,
      data: params
    });

116
    return params;
D
devil_gong 已提交
117 118 119 120 121 122
  },

  /**
   * 获取设备信息
   */
  get_system_info() {
D
devil_gong 已提交
123 124
    let system_info = wx.getStorageSync(this.data.cache_system_info_key) || null;
    if (system_info == null) {
D
devil_gong 已提交
125 126
      return this.set_system_info();
    }
D
devil_gong 已提交
127
    return system_info;
D
devil_gong 已提交
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
  },

  /**
   * 设置设备信息
   */
  set_system_info() {
    var system_info = wx.getSystemInfoSync();
    wx.setStorage({
      key: this.data.cache_system_info_key,
      data: system_info
    });
    return system_info;
  },

  /**
   * 请求地址生成
D
devil 已提交
144 145 146 147
   * a              方法
   * c              控制器
   * plugins        插件标记(传参则表示为插件请求)
   * params         url请求参数
D
devil_gong 已提交
148
   */
D
devil 已提交
149
  get_request_url(a, c, plugins, params) {
D
devil_gong 已提交
150 151
    a = a || "index";
    c = c || "index";
D
devil 已提交
152 153 154 155 156 157 158 159 160 161 162 163 164

    // 是否插件请求
    var plugins_params = "";
    if ((plugins || null) != null)
    {
      plugins_params = "&pluginsname=" + plugins + "&pluginscontrol=" + c + "&pluginsaction=" + a;

      // 走api统一插件调用控制器
      c = "plugins"
      a = "index"
    }

    // 参数处理
D
devil_gong 已提交
165 166 167 168
    params = params || "";
    if (params != "" && params.substr(0, 1) != "&") {
      params = "&" + params;
    }
D
devil 已提交
169 170

    // 用户信息
D
devil_gong 已提交
171
    var user = this.get_user_cache_info();
D
devil_gong 已提交
172
    var token = (user == false) ? '' : user.token || '';
D
devil 已提交
173
    var uuid = this.request_uuid();
D
devil 已提交
174 175
    return this.data.request_url +
      "index.php?s=/api/" + c + "/" + a + plugins_params+
D
devil_gong 已提交
176
      "&application=app&application_client_type=weixin" +
D
devil 已提交
177
      "&token=" + token +
D
devil_gong 已提交
178
      "&ajax=ajax" +
D
devil 已提交
179
      "&uuid="+ uuid +
D
devil 已提交
180
      params;
D
devil_gong 已提交
181 182
  },

D
devil_gong 已提交
183 184 185 186 187 188 189 190 191 192
  /**
   * 获取用户信息,信息不存在则唤醒授权
   * object     回调操作对象
   * method     回调操作对象的函数
   * return     有用户数据直接返回, 则回调调用者
   */
  get_user_info(object, method) {
    var user = this.get_user_cache_info();
    if (user == false) {
      // 唤醒用户授权
D
devil_gong 已提交
193
      this.user_login(object, method);
D
devil_gong 已提交
194 195 196 197 198 199 200

      return false;
    } else {
      return user;
    }
  },

D
devil_gong 已提交
201 202 203
  /**
   * 从缓存获取用户信息
   */
D
devil_gong 已提交
204
  get_user_cache_info() {
D
devil_gong 已提交
205 206
    let user = wx.getStorageSync(this.data.cache_user_info_key) || null;
    if (user == null) {
D
devil_gong 已提交
207 208 209 210 211 212 213 214 215
      return false;
    }
    return user;
  },

  /**
   * 用户登录
   * object     回调操作对象
   * method     回调操作对象的函数
D
devil_gong 已提交
216
   * auth_data  授权数据
D
devil_gong 已提交
217
   */
G
gongfuxiang 已提交
218
  user_auth_login(object, method, auth_data) {
D
devil_gong 已提交
219
    var self = this;
D
devil_gong 已提交
220 221
    wx.checkSession({
      success: function () {
D
devil_gong 已提交
222
        var openid = wx.getStorageSync(self.data.cache_user_login_key) || null;
D
devil_gong 已提交
223
        if (openid == null)
D
devil_gong 已提交
224
        {
D
devil_gong 已提交
225
          self.user_login(object, method);
D
devil_gong 已提交
226
        } else {
D
devil_gong 已提交
227
          self.get_user_login_info(object, method, openid, auth_data);
D
devil_gong 已提交
228 229 230
        }
      },
      fail: function () {
D
devil_gong 已提交
231
        self.user_login(object, method);
D
devil_gong 已提交
232 233 234 235 236 237 238 239
      }
    });
  },

  /**
   * 用户登录
   * object     回调操作对象
   * method     回调操作对象的函数
G
gongfuxiang 已提交
240
   * auth_data  授权数据
D
devil_gong 已提交
241
   */
D
devil_gong 已提交
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
  user_login(object, method) {
    var openid = wx.getStorageSync(this.data.cache_user_login_key) || null;
    if (openid == null)
    {
      var self = this;
      // 加载loding
      wx.showLoading({ title: "授权中..." });

      wx.login({
        success: (res) => {
          if (res.code) {
            wx.request({
              url: self.get_request_url('wechatuserauth', 'user'),
              method: 'POST',
              data: { authcode: res.code },
              dataType: 'json',
              header: { 'content-type': 'application/x-www-form-urlencoded' },
              success: (res) => {
                wx.hideLoading();
                if (res.data.code == 0) {
                  var data = res.data.data;
D
devil 已提交
263
                  if ((data.is_user_exist || 0) == 1) {
D
devil_gong 已提交
264 265 266 267 268 269 270 271 272 273
                    wx.setStorage({
                      key: self.data.cache_user_info_key,
                      data: data,
                      success: (res) => {
                        if (typeof object === 'object' && (method || null) != null) {
                          object[method]();
                        }
                      },
                      fail: () => {
                        self.showToast('用户信息缓存失败');
D
devil_gong 已提交
274
                      }
D
devil_gong 已提交
275 276 277 278 279 280 281 282
                    });
                  } else {
                    wx.setStorage({
                      key: self.data.cache_user_login_key,
                      data: data.openid
                    });
                    self.login_to_auth();
                  }
D
devil_gong 已提交
283
                } else {
D
devil_gong 已提交
284 285
                  wx.hideLoading();
                  self.showToast(res.data.msg);
D
devil_gong 已提交
286
                }
D
devil_gong 已提交
287 288
              },
              fail: () => {
D
devil_gong 已提交
289
                wx.hideLoading();
D
devil_gong 已提交
290 291 292 293 294 295 296 297
                self.showToast('服务器请求出错');
              },
            });
          }
        },
        fail: (e) => {
          wx.hideLoading();
          self.showToast('授权失败');
D
devil_gong 已提交
298
        }
D
devil_gong 已提交
299 300 301 302 303 304 305 306 307 308 309
      });
    } else {
      this.login_to_auth();
    }
  },

  /**
   * 跳转到登录页面授权
   */
  login_to_auth() {
    wx.showModal({
D
Devil 已提交
310 311 312 313 314 315 316 317 318
      title: '温馨提示',
      content: '授权用户信息',
      confirmText: '确认',
      cancelText: '暂不',
      success: (result) => {
        if (result.confirm) {
          wx.navigateTo({
            url: "/pages/login/login"
          });
D
devil_gong 已提交
319
        }
D
Devil 已提交
320 321
      }
    });
D
devil_gong 已提交
322 323 324 325 326 327 328
  },

  /**
   * 获取用户授权信息
   * object     回调操作对象
   * method     回调操作对象的函数
   * openid     用户openid
G
gongfuxiang 已提交
329
   * auth_data  授权数据
D
devil_gong 已提交
330
   */
G
gongfuxiang 已提交
331
  get_user_login_info(object, method, openid, auth_data) {
G
gongfuxiang 已提交
332
    // 邀请人参数
D
devil_gong 已提交
333
    var params = wx.getStorageSync(this.data.cache_launch_info_key) || null;
334
    var referrer = (params == null) ? 0 : (params.referrer || 0);
G
gongfuxiang 已提交
335

G
gongfuxiang 已提交
336
    // 远程解密数据
D
devil_gong 已提交
337
    wx.showLoading({ title: "授权中..." });
D
devil_gong 已提交
338
    var self = this;
G
gongfuxiang 已提交
339
    wx.request({
D
devil_gong 已提交
340
      url: self.get_request_url('wechatuserinfo', 'user'),
G
gongfuxiang 已提交
341
      method: 'POST',
G
gongfuxiang 已提交
342 343 344 345 346 347
      data: {
        "encrypted_data": auth_data.encryptedData,
        "iv": auth_data.iv,
        "openid": openid,
        "referrer": referrer
      },
G
gongfuxiang 已提交
348 349 350 351 352 353
      dataType: 'json',
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      success: (res) => {
        wx.hideLoading();
        if (res.data.code == 0) {
          wx.setStorage({
D
devil_gong 已提交
354
            key: self.data.cache_user_info_key,
G
gongfuxiang 已提交
355 356 357 358 359 360 361
            data: res.data.data,
            success: (res) => {
              if (typeof object === 'object' && (method || null) != null) {
                object[method]();
              }
            },
            fail: () => {
D
devil_gong 已提交
362
              self.showToast('用户信息缓存失败');
D
devil_gong 已提交
363
            }
G
gongfuxiang 已提交
364 365
          });
        } else {
D
devil_gong 已提交
366
          self.showToast(res.data.msg);
G
gongfuxiang 已提交
367
        }
D
devil_gong 已提交
368
      },
G
gongfuxiang 已提交
369
      fail: () => {
D
devil_gong 已提交
370
        wx.hideLoading();
D
devil_gong 已提交
371
        self.showToast('服务器请求出错');
G
gongfuxiang 已提交
372
      },
D
devil_gong 已提交
373 374 375 376 377 378
    });
  },

  /**
   * 字段数据校验
   * data           待校验的数据, 一维json对象
D
devil_gong 已提交
379
   * validation     待校验的字段, 格式 [{fields: 'mobile', msg: '请填写手机号码', is_can_zero: 1(是否可以为0)}, ...]
D
devil_gong 已提交
380 381 382
  */
  fields_check(data, validation) {
    for (var i in validation) {
D
Devil 已提交
383 384 385 386 387
      var temp_value = data[validation[i]["fields"]];
      var temp_is_can_zero = validation[i]["is_can_zero"] || null;

      if ((temp_value == undefined || temp_value.length == 0 || temp_value == -1) || (temp_is_can_zero == null && temp_value == 0)
      ) {
D
devil_gong 已提交
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
        this.showToast(validation[i]['msg']);
        return false;
      }
    }
    return true;
  },

  /**
   * 获取当前时间戳
   */
  get_timestamp() {
    return parseInt(new Date().getTime() / 1000);
  },

  /**
   * 获取日期
   * format       日期格式(默认 yyyy-MM-dd h:m:s)
   * timestamp    时间戳(默认当前时间戳)
   */
  get_date(format, timestamp) {
    var d = new Date((timestamp || this.get_timestamp()) * 1000);
    var date = {
      "M+": d.getMonth() + 1,
      "d+": d.getDate(),
      "h+": d.getHours(),
      "m+": d.getMinutes(),
      "s+": d.getSeconds(),
      "q+": Math.floor((d.getMonth() + 3) / 3),
      "S+": d.getMilliseconds()
    };
    if (/(y+)/i.test(format)) {
      format = format.replace(RegExp.$1, (d.getFullYear() + '').substr(4 - RegExp.$1.length));
    }
    for (var k in date) {
      if (new RegExp("(" + k + ")").test(format)) {
        format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
      }
    }
    return format;
  },

  /**
   * 获取对象、数组的长度、元素个数
   * obj      要计算长度的元素(object、array、string)
   */
  get_length(obj) {
    var obj_type = typeof obj;
    if (obj_type == "string") {
      return obj.length;
    } else if (obj_type == "object") {
      var obj_len = 0;
      for (var i in obj) {
        obj_len++;
      }
      return obj_len;
    }
    return false;
  },

  /**
   * 价格保留两位小数
   * price      价格保留两位小数
   */
  price_two_decimal(x) {
    var f_x = parseFloat(x);
    if (isNaN(f_x)) {
      return 0;
    }
    var f_x = Math.round(x * 100) / 100;
    var s_x = f_x.toString();
    var pos_decimal = s_x.indexOf('.');
    if (pos_decimal < 0) {
      pos_decimal = s_x.length;
      s_x += '.';
    }
    while (s_x.length <= pos_decimal + 2) {
      s_x += '0';
    }
    return s_x;
  },

G
gongfuxiang 已提交
469 470 471 472 473 474
  /**
   * 当前地址是否存在tabbar中
   */
  is_tabbar_pages(url) {
    if (url.indexOf("?") == -1)
    {
D
devil 已提交
475
      var value = url;
G
gongfuxiang 已提交
476 477
    } else {
      var temp_str = url.split("?");
D
devil 已提交
478
      var value = temp_str[0];
G
gongfuxiang 已提交
479
    }
D
devil 已提交
480
    if ((value || null) == null)
G
gongfuxiang 已提交
481 482 483 484 485 486 487
    {
      return false;
    }

    var temp_tabbar_pages = this.data.tabbar_pages;
    for (var i in temp_tabbar_pages)
    {
D
devil 已提交
488
      if (temp_tabbar_pages[i] == value)
G
gongfuxiang 已提交
489 490 491 492 493 494 495
      {
        return true;
      }
    }
    return false;
  },

G
wechat  
gongfuxiang 已提交
496 497 498 499 500 501 502 503 504 505
  /**
   * 事件操作
   */
  operation_event(e) {
    var value = e.currentTarget.dataset.value || null;
    var type = parseInt(e.currentTarget.dataset.type);
    if (value != null) {
      switch (type) {
        // web
        case 0:
G
gongfuxiang 已提交
506
          wx.navigateTo({ url: '/pages/web-view/web-view?url=' + encodeURIComponent(value) });
G
wechat  
gongfuxiang 已提交
507 508 509 510
          break;

        // 内部页面
        case 1:
G
gongfuxiang 已提交
511 512 513 514 515 516
          if (this.is_tabbar_pages(value))
          {
            wx.switchTab({ url: value });
          } else {
            wx.navigateTo({ url: value });
          }
G
wechat  
gongfuxiang 已提交
517 518 519 520 521 522 523 524 525 526 527
          break;

        // 跳转到外部小程序
        case 2:
          wx.navigateToMiniProgram({ appId: value });
          break;

        // 跳转到地图查看位置
        case 3:
          var values = value.split('|');
          if (values.length != 4) {
D
devil_gong 已提交
528
            this.showToast('事件值格式有误');
G
wechat  
gongfuxiang 已提交
529 530
            return false;
          }
D
devil 已提交
531
          this.open_location(values[2], values[3], values[0], values[1]);
G
wechat  
gongfuxiang 已提交
532 533 534 535
          break;

        // 拨打电话
        case 4:
D
devil 已提交
536
          this.call_tel(value);
G
wechat  
gongfuxiang 已提交
537 538 539 540 541
          break;
      }
    }
  },

D
devil_gong 已提交
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
  /**
   * 默认弱提示方法
   * msg    [string]  提示信息
   * status [string]  状态 默认error [正确success, 错误error]
   */
  showToast(msg, status)
  {
    if ((status || 'error') == 'success')
    {
      wx.showToast({
        title: msg,
        duration: 3000
      });
    } else {
      wx.showToast({
        image: '/images/default-toast-error.png',
        title: msg,
        duration: 3000
      });
    }
D
1.6  
devil_gong 已提交
562 563
  },

D
devil 已提交
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
  /**
   * alert确认框
   * title              [string]    标题(默认空)
   * msg                [string]    提示信息,必传
   * is_show_cancel     [int]       是否显示取消按钮(默认显示 0否, 1|undefined是)
   * cancel_text        [string]    取消按钮文字(默认 取消)
   * cancel_color       [string]    取消按钮的文字颜色,必须是 16 进制格式的颜色字符串(默认 #000000)
   * confirm_text       [string]    确认按钮文字(默认 确认)
   * confirm_color      [string]    确认按钮的文字颜色,必须是 16 进制格式的颜色字符串(默认 #000000)
   * object             [boject]    回调操作对象,点击确认回调参数1,取消回调0
   * method             [string]    回调操作对象的函数
   */
  alert(e)
  {
    var msg = e.msg || null;
    if (msg != null)
    {
      var title = e.title || '';
      var is_show_cancel = (e.is_show_cancel == 0) ? false : true;
      var cancel_text = e.cancel_text || '取消';
      var confirm_text = e.confirm_text || '确认';
D
devil 已提交
585 586
      var cancel_color = e.cancel_color || '#000000';
      var confirm_color = e.confirm_color || '#576B95';
D
devil 已提交
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606

      wx.showModal({
        title: title,
        content: msg,
        showCancel: is_show_cancel,
        cancelText: cancel_text,
        cancelColor: cancel_color,
        confirmText: confirm_text,
        confirmColor: confirm_color,
        success(res) {
          if ((e.object || null) != null && typeof e.object === 'object' && (e.method || null) != null) {
            e.object[e.method](res.confirm ? 1 : 0);
          }
        }
      });
    } else {
      self.showToast('提示信息为空 alert');
    }
  },

D
1.6  
devil_gong 已提交
607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627
  /**
   * 是否需要登录
   * 是否需要绑定手机号码
   */
  user_is_need_login(user) {
    // 用户信息是否正确
    if (user == false)
    {
      return true;
    }

    // 是否需要绑定手机号码
    if ((user.is_mandatory_bind_mobile || 0) == 1)
    {
      if ((user.mobile || null) == null)
      {
        return true;
      }
    }
    
    return false;
628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643
  },

  /**
   * url参数转json对象
   */
  url_params_to_json(url_params) {
    var json = new Object();
    if ((url_params || null) != null)
    {
      var arr = url_params.split('&');
      for(var i = 0; i<arr.length; i++) {
      var temp = arr[i].split('=');
        json[temp[0]] = temp[1]
      }
    }
    return json;
D
devil_gong 已提交
644 645 646 647 648 649 650 651
  },

  // 拨打电话
  call_tel(value) {
    if ((value || null) != null) {
      wx.makePhoneCall({ phoneNumber: value });
    }
  },
D
devil_gong 已提交
652

D
devil_gong 已提交
653 654 655 656 657 658
  /**
   * 登录校验
   * object     回调操作对象
   * method     回调操作对象的函数
   */
  is_login_check(res, object, method) {
D
devil_gong 已提交
659 660 661
    if(res.code == -400)
    {
      wx.clearStorage();
D
devil_gong 已提交
662
      this.get_user_info(object, method);
D
devil_gong 已提交
663 664 665 666 667
      return false;
    }
    return true;
  },

D
devil 已提交
668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696
  /**
   * 设置导航reddot
   * index     tabBar 的哪一项,从左边算起(0开始)
   * type      0 移出, 1 添加 (默认 0 移出)
   */
  set_tab_bar_reddot(index, type) {
    if (index !== undefined && index !== null)
    {
      if ((type || 0) == 0)
      {
        wx.hideTabBarRedDot({ index: Number(index) });
      } else {
        wx.showTabBarRedDot({ index: Number(index) });
      }
    }
  },

  /**
   * 设置导航车badge
   * index     tabBar 的哪一项,从左边算起(0开始)
   * type      0 移出, 1 添加 (默认 0 移出)
   * value     显示的文本,超过 4 个字符则显示成 ...(type参数为1的情况下有效)
   */
  set_tab_bar_badge(index, type, value) {
    if (index !== undefined && index !== null)
    {
      if ((type || 0) == 0) {
        wx.removeTabBarBadge({ index: Number(index) });
      } else {
D
devil 已提交
697
        wx.setTabBarBadge({ index: Number(index), "text": value.toString() });
D
devil 已提交
698 699 700 701
      }
    }
  },

D
devil 已提交
702 703 704 705 706 707 708 709
  // 显示分享菜单
  show_share_menu() {
    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline']
    });
  },

D
devil 已提交
710 711 712 713 714 715 716
  /**
   * 获取配置信息、可指定默认值
   * key              数据key(支持多级读取、以 . 分割key名称)
   * default_value    默认值
   */
  get_config(key, default_value) {
    var value = null;
D
devil 已提交
717
    var config = wx.getStorageSync(this.data.cache_config_info_key) || null;
D
devil 已提交
718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794
    if(config != null)
    {
      // 数据读取
      var arr = key.split('.');
      if(arr.length == 1)
      {
        value = config[key] == undefined ? null : config[key];
      } else {
        value = config;
        for(var i in arr)
        {
          if(value[arr[i]] != undefined)
          {
            value = value[arr[i]];
          } else {
            value =  null;
            break;
          }
        }
      }
    }
    return (value === null) ? ((default_value === undefined) ? value : default_value) : value;
  },

  // 初始化 配置信息
  init_config() {
    var self = this;
    wx.request({
      url: this.get_request_url('common', 'base'),
      method: 'POST',
      data: {},
      dataType: 'json',
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      success: (res) => {
        if (res.data.code == 0) {
          wx.setStorage({
            key: this.data.cache_config_info_key,
            data: res.data.data,
            fail: () => {
              this.showToast('配置信息缓存失败');
            }
          });
        } else {
          this.showToast(res.data.msg);
        }
      },
      fail: () => {
        this.showToast('服务器请求出错');
      },
    });
  },

  /**
   * 配置是否有效(100毫秒检验一次、最多检验100次)
   * object     回调操作对象
   * method     回调操作对象的函数
   */
  is_config(object, method) {
    var self = this;
    var count = 0;
    var timer = setInterval(function()
    {
      if(self.get_config('status') == 1)
      {
        clearInterval(timer);
        if (typeof object === 'object' && (method || null) != null) {
          object[method](true);
        }
      }
      count++;
      if(count >= 100)
      {
        clearInterval(timer);
      }
    }, 100);
  },

D
devil 已提交
795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837
  /**
   * 百度坐标BD-09到火星坐标GCJ02(高德,谷歌,腾讯坐标)
   * object     回调操作对象
   * method     回调操作对象的函数
   */
  map_bd_to_gcj(lng, lat) {
    let x_pi = 3.14159265358979324 * 3000.0 / 180.0;
    let x = lng - 0.0065;
    let y = lat - 0.006;
    let z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi);
    let theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi);
    let lngs = z * Math.cos(theta);
    let lats = z * Math.sin(theta);
    return {
      lng: lngs,
      lat: lats
    };
  },

  /**
   * 百度坐标BD-09到火星坐标GCJ02(高德,谷歌,腾讯坐标)
   * lng        经度
   * lat        纬度
   * name       地图上面显示的名称
   * address    地图上面显示的详细地址
   * scale      缩放比例,范围5~18
   */
  open_location(lng, lat, name, address, scale) {
    if(lng == undefined || lat == undefined || lng == '' || lat == '') {
      this.showToast('坐标有误');
      return false;
    }

    // 转换坐标打开位置
    var position = this.map_bd_to_gcj(parseFloat(lng), parseFloat(lat));
    wx.openLocation({
      name: name || '',
      address: address || '',
      scale: scale || 18,
      longitude: position.lng,
      latitude: position.lat
    });
  },
D
devil 已提交
838

D
devil 已提交
839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864
  // uuid生成
  uuid() {
    var d = new Date().getTime();
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
      var r = (d + Math.random() * 16) % 16 | 0;
      d = Math.floor(d / 16);
      return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16);
    });
  },

  // 获取当前uuid
  request_uuid() {
    var uuid = wx.getStorageSync(this.data.cache_user_uuid_key) || null;
    if(uuid == null) {
      uuid = this.uuid();
      wx.setStorage({
        key: this.data.cache_user_uuid_key,
        data: uuid,
        fail: () => {
          this.showToast('uuid缓存失败');
        }
      });
    }
    return uuid;
  },

D
devil_gong 已提交
865
});