common.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import BaseStore from '@src/stores/base';
  2. import { generateUUID } from '@src/services/Tools';
  3. import { WechatH5AppId } from '../../Constant';
  4. export default class CommonStore extends BaseStore {
  5. /**
  6. * 发送短信验证码
  7. * @param {*} area 区域码
  8. * @param {*} mobile 手机号
  9. */
  10. sendSms(area, mobile) {
  11. return this.apiPost('/common/sms/valid', { area, mobile });
  12. }
  13. /**
  14. * 上传至本地服务器
  15. * @param {*} file 图片文件
  16. */
  17. upload(file) {
  18. return this.apiForm('/common/upload/image', { file });
  19. }
  20. readyWechatBridge() {
  21. return new Promise((resolve) => {
  22. if (typeof WeixinJSBridge === 'object' && typeof WeixinJSBridge.invoke === 'function') {
  23. resolve();
  24. } else if (document.addEventListener) {
  25. document.addEventListener('WeixinJSBridgeReady', resolve, false);
  26. } else if (document.attachEvent) {
  27. document.attachEvent('WeixinJSBridgeReady', resolve);
  28. document.attachEvent('onWeixinJSBridgeReady', resolve);
  29. }
  30. });
  31. }
  32. readyWechat(url, list) {
  33. return this.apiGet('/common/wechat/ticket').then(ticket => {
  34. const time = parseInt(new Date().getTime() / 1000, 10);
  35. const nonce = generateUUID(6, 10);
  36. const p = `jsapi_ticket=${ticket}&noncestr=${nonce}&timestamp=${time}&url=${url.split('#')[0]}`;
  37. const a = list.map(row => row);
  38. a.push('checkJsApi');
  39. wx.config({
  40. debug: false, // 是否打开调试模式,调用的api会被alert出来,在pc端也能看到log信息
  41. appId: WechatH5AppId, // 必填,微信公众号的唯一标识
  42. timestamp: time, // 必填,生成签名的时间戳
  43. nonceStr: nonce, // 必填,生成签名的随机串
  44. signature: sha1(p), // 必填,用于验证的签名
  45. jsApiList: a, // 必填,需要使用到的JS接口列表
  46. });
  47. return new Promise((resolve, reject) => {
  48. wx.ready(() => {
  49. resolve(wx);
  50. });
  51. wx.error((err) => {
  52. reject(err);
  53. });
  54. });
  55. });
  56. }
  57. }
  58. export const Common = new CommonStore({ key: 'common' });