common.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import BaseStore from '@src/stores/base';
  2. import { generateUUID } from '@src/services/Tools';
  3. import { WechatAppId } 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(callback) {
  21. if (typeof WeixinJSBridge === 'object' && typeof WeixinJSBridge.invoke === 'function') {
  22. callback();
  23. } else if (document.addEventListener) {
  24. document.addEventListener('WeixinJSBridgeReady', callback, false);
  25. } else if (document.attachEvent) {
  26. document.attachEvent('WeixinJSBridgeReady', callback);
  27. document.attachEvent('onWeixinJSBridgeReady', callback);
  28. }
  29. }
  30. readyWechat(url, list) {
  31. return this.apiGet('/common/wechat/ticket').then(ticket => {
  32. const time = new Date().getTime() / 1000;
  33. const nonce = generateUUID(6, 10);
  34. const p = `jsapi_ticket=${ticket}&noncestr=${nonce}&timestamp=${time}&url=${url.split('#')[0]}`;
  35. const a = list.map(row => row);
  36. a.push('checkJsApi');
  37. wx.config({
  38. debug: false, // 是否打开调试模式,调用的api会被alert出来,在pc端也能看到log信息
  39. appid: WechatAppId, // 必填,微信公众号的唯一标识
  40. timestamp: time, // 必填,生成签名的时间戳
  41. nonceStr: nonce, // 必填,生成签名的随机串
  42. signature: sha1(p), // 必填,用于验证的签名
  43. jsApiList: a, // 必填,需要使用到的JS接口列表
  44. });
  45. return new Promise((resolve, reject) => {
  46. wx.ready(() => {
  47. resolve(wx);
  48. });
  49. wx.error((err) => {
  50. reject(err);
  51. });
  52. });
  53. });
  54. }
  55. }
  56. export const Common = new CommonStore({ key: 'common' });