request.js 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010
  1. const utils = require('./util.js');
  2. const h5util = require('./h5util.js');
  3. const api = require('./api.js');
  4. const app = getApp();
  5. // let WebIM = (wx.WebIM = require("../hxChatSDK/utils/WebIM")["default"]);
  6. /**
  7. * H5版的发布地址目录:148服务器
  8. * /data/web/c-h5.lvluocloud.com
  9. * 域名:c-h5.lvluocloud.com
  10. */
  11. // 测试环境域名 http://c-h5.ts.zhiqiyun.com
  12. const env = {
  13. NODE_ENV: 'dev',
  14. dev: {
  15. apiUrl: 'https://apitest.zhiqiyun.com',
  16. isSolution: true,
  17. shareAppid:'ZQ4KaYIMHTypO0T'
  18. // apiUrl: 'https://apitest.tongyu99.com'
  19. },
  20. prd: {
  21. apiUrl: 'https://apimall.zhiqiyun.com',
  22. isSolution: true,
  23. shareAppid:'ZQCK1g7w7NKYAYP'
  24. },
  25. pota: {
  26. // apiUrl: 'http://192.168.110.59:8098' //f
  27. // apiUrl: 'http://192.168.110.176:8098' ,//yx
  28. apiUrl: 'http://192.168.110.195:8098', //xw
  29. // apiUrl: 'http://192.168.110.182:8098' ,//zf
  30. isSolution: true,
  31. shareAppid:'ZQ4KaYIMHTypO0T'
  32. }
  33. }
  34. var header = {
  35. 'content-type': 'application/x-www-form-urlencoded',
  36. 'appId': 'ZQ4KaYIMHTypO0T', //绿萝云测试
  37. // 'appId': 'ZQKmmjlYyTRmssC', //绿萝云正式
  38. // 'appId': 'ZQDGetYBDwHbfaP',//绿萝大药房测试
  39. // 'appId': 'ZQKmmjlYyTRmssC',//绿萝云正式
  40. // 'appId': 'ZQDGetYBDwHbfaP',//绿萝大药房测试
  41. // 'appId': 'ZQDAvgsQ4P51n1b',//绿萝大药房正式独立租户
  42. // 'appId': 'ZQjtblxKOibbZsE',//绿萝社区诊所测试
  43. // 'appId': 'ZQHGg8rQYqkcKvV',//绿萝中医馆测试
  44. // 'appId': 'ZQP48U6KeLNQZXN',//绿萝医疗美容测试
  45. // 'appId': 'ZQaw2Mgigmwsxwr',//绿萝专科门诊测试
  46. // 'appId': 'ZQrbSJm5S694cXo',//泽惠云测试
  47. // 'appId': 'ZQLyCKPHBHUTgpj',//泽惠云正式
  48. // 'appId': 'ZQYLXwCoEdE13j9',//桃燕禧测试
  49. // 'appId':'ZQuNAVEkY3ClG2D',//桃燕禧正式
  50. // 'appId':'ZQ6XiKauuAoSyfS',//金药堂
  51. // 'appId':'ZQ17g14A1SXrlNU',//本吉口腔
  52. }
  53. /**小程序配置 */
  54. const publics = {
  55. WX_GZH_APPID: 'wx891371f1700a6806',
  56. WX_GZH_URI: 'http://h5.cheese-vip.com/comm/login/login',
  57. 'weixinGzAppid': 'wxf3bf18d2889d98ad', //默认公众号appid
  58. // 'appId': 'ZQCK1g7w7NKYAYP', //正式固定的appid(绿萝云助手运营后台)
  59. 'appId': env[env.NODE_ENV].shareAppid, //测试绿萝云运营平台appid(系统运营租户)
  60. picUrls: 'https://zhiqiyun.oss-cn-hangzhou.aliyuncs.com/static/', //图片地址
  61. 'copyrighttitle': '智企云', //底部版权公司名称
  62. 'copyrighttel': '400-698-5980', //底部版权服务电话
  63. 'copyrightlogo': 'zhiqiyun', //底部版权 logo 文件名
  64. 'yaomeng': 'https://cfwz.yaomengwang.cn/hunan?enterpriseCode=hnty&orderId=', //药盟接口地址
  65. 'storemanagers': 'https://websmall.zhiqiyun.com?appId=', //saas门店管理地址(智企云所有)
  66. 'channel': '', //处方中间渠道号 SAAS系统为空
  67. 'is_customer_pick_up': false,
  68. mapLBSKEY: 'XMCBZ-7K3WN-JD3FA-SDBGN-ZYVU3-MZBQZ',
  69. 'iconSrc': 'blue/',
  70. 'titleTopBgColor': '#1a83ff',
  71. //小程序ID wx987a09d18df068be
  72. //药店版模板小程序:wxddcf0549757d4f61
  73. //中医馆方案模板小程序:wxccea26be900083b6
  74. // 'mapLBSKEY': 'B32BZ-KIP6Q-6IL57-4XOK2-EYOKV-4SBA5',//泽惠云
  75. // 'iconSrc': 'blue/',
  76. // 'titleTopBgColor': '#1a83ff',
  77. //小程序ID wx967c93213cd77e0d
  78. // 桃燕禧小程序ID
  79. // 'mapLBSKEY': 'KWKBZ-BVJWJ-QIGFM-DEITX-56FZ5-OGFSH',
  80. //wxb352884750204cd3
  81. // 金药堂小程序ID
  82. // 'mapLBSKEY': 'LWZBZ-K7WWQ-52L56-2ZCIE-F4PJO-2NBU5',
  83. //wxbb4bc21cf658d44d
  84. // 本吉口腔小程序ID
  85. // 'mapLBSKEY': 'GIKBZ-H6JKT-DLTXX-VO4HD-GPRFJ-7TFHI',
  86. //wxa1ba85dfdafad298
  87. }
  88. function getHeader(content_type, appID) {
  89. var appId = '';
  90. if (appID) {
  91. appId = appID;
  92. } else {
  93. appId = getStorage('appId');
  94. if (!appId) {
  95. appId = header.appId;
  96. setStorage('appId', appId)
  97. }
  98. }
  99. var headers = {
  100. "content-type": content_type ? content_type : 'application/x-www-form-urlencoded',
  101. "appId": appId,
  102. };
  103. return headers;
  104. }
  105. /*
  106. * 获取token
  107. */
  108. const getToken = (success, appId, isKDX) => {
  109. var token = '';
  110. if (isKDX) {
  111. token = getStorage('S_AUTH_TOKEN');
  112. } else {
  113. token = getStorage('AUTH_TOKEN');
  114. }
  115. // let token = 'JKoXWFCY1yB93BQdLVb9BcibWAaexceyR';
  116. if (token) {
  117. success.call(this, token);
  118. return false;
  119. } else {
  120. loadToken(data => {
  121. success.call(this, data);
  122. }, appId, isKDX);
  123. }
  124. }
  125. const loadToken = (success, appId, isKDX) => {
  126. let that = this
  127. uni.request({
  128. url: env[env.NODE_ENV].apiUrl + '/api/access_token',
  129. data: {},
  130. method: 'get',
  131. header: getHeader('', appId),
  132. success(json) {
  133. if (json.statusCode !== 200) return msg(json.data.message);
  134. if (json.data.code !== 0) return msg(json.data.msg)
  135. if (isKDX) {
  136. setStorage('S_AUTH_TOKEN', json.data.data);
  137. } else {
  138. setStorage('AUTH_TOKEN', json.data.data);
  139. }
  140. success.call(that, json.data.data);
  141. },
  142. fail() {
  143. msg('access_token 获取失败');
  144. }
  145. })
  146. }
  147. /**
  148. * 微信小程序静默授权登录
  149. * @param {*} parentId
  150. * @param {*} merchantId
  151. * @param {*} type 用户来源类型 1 小程序搜索 2 活动报名 3 名片分享 4商品分享 5店铺分享 6文章分享 7小程序分享 8素材分享 9公众号分享 10公众号搜索 11企微活码 12微信活码 13手工录入 14 5G消息进入
  152. */
  153. const silenceLogin = (parentId, merchantId, type) => {
  154. return new Promise(async(resolve, reject) => {
  155. let logins =await isAuthFn(false)
  156. if (!logins) { //未登录
  157. // #ifdef MP-WEIXIN || MP-TOUTIAO
  158. uni.login({
  159. success(res) {
  160. console.log('静默授权登录>>>', res);
  161. let params = {
  162. code: res.code,
  163. parentId: parentId ? parentId : '',
  164. merchantId: merchantId ? merchantId : ''
  165. }
  166. var url = '';
  167. // #ifdef MP-WEIXIN
  168. url = '/api/v3/silence/login';
  169. // #endif
  170. // #ifdef MP-TOUTIAO
  171. url = '/api/v3/tiktok/login';
  172. // #endif
  173. postRequest(url, params, data => {
  174. if (data.token) setStorage('AUTH_TOKEN', data.token);
  175. setStorage('userInfo', data);
  176. savePortAndSource({
  177. uid: getStorage('userInfo').id,
  178. source: type
  179. })
  180. // getRequest('/api/config',{},data=>{
  181. // if(data.is_not_open_im != 1) getIMToken(false);
  182. // })
  183. resolve()
  184. });
  185. }
  186. });
  187. // #endif
  188. // #ifdef H5
  189. //判断是否为微信内环境,只有内环境才执行静默授权登录
  190. // env.NODE_ENV=='prd' 只有正式环境域名可静默授权,如果是测试环境仍用手机号登陆
  191. if (utils.isWeixin() && env.NODE_ENV=='prd') {
  192. console.log('$$$$$$$$微信环境,进入静默授权')
  193. getApp().wxOauth(parentId, merchantId, 'snsapi_base');
  194. resolve()
  195. }else{
  196. resolve()
  197. }
  198. // #endif
  199. } else {
  200. // getRequest('/api/config',{},data=>{
  201. // if(data.is_not_open_im != 1) getIMToken(false);
  202. // })
  203. // initIMChat();
  204. savePortAndSource({
  205. uid: getStorage('userInfo').id,
  206. source: type
  207. })
  208. resolve()
  209. }
  210. })
  211. }
  212. const onNetworkStatusChangeIM = () => {
  213. // console.log('IM>>>>>>>>>>>>>网络变化调用');
  214. // uni.onNetworkStatusChange(info => {
  215. // msg('切换了网络,开始重连');
  216. // WebIM.conn.close();
  217. // setTimeout(() => {
  218. // initIMChat();
  219. // }, 500);
  220. // });
  221. }
  222. /**
  223. * 初始化 IMsdk
  224. */
  225. const initIMChat = () => {
  226. var user = getStorage('userInfo');
  227. let myUsername = uni.getStorageSync("myUsername")
  228. var imToken = getStorage('imToken');
  229. console.log(imToken)
  230. if (imToken) {
  231. if (myUsername) {
  232. getApp().globalData.conn.open({
  233. apiUrl: WebIM.config.apiURL,
  234. user: myUsername,
  235. pwd: getStorage('imToken'),
  236. accessToken: getStorage('imToken'),
  237. appKey: WebIM.config.appkey
  238. });
  239. } else {
  240. getRequest('/api/v3/im/em/userId', {
  241. userId: user.id
  242. }, suc => {
  243. uni.setStorage({
  244. key: "myUsername",
  245. data: 'RS_' + suc
  246. });
  247. getApp().globalData.conn.open({
  248. apiUrl: WebIM.config.apiURL,
  249. user: 'RS_' + suc,
  250. pwd: getStorage('imToken'),
  251. accessToken: getStorage('imToken'),
  252. appKey: WebIM.config.appkey
  253. });
  254. });
  255. }
  256. } else {
  257. getRequest('/api/v3/im/em/userToken', {}, suc => {
  258. setStorage('imToken', suc.token);
  259. uni.setStorage({
  260. key: "myUsername",
  261. data: 'RS_' + suc.id
  262. });
  263. getApp().globalData.conn.open({
  264. apiUrl: WebIM.config.apiURL,
  265. user: suc.id,
  266. pwd: getStorage('imToken'),
  267. accessToken: getStorage('imToken'),
  268. appKey: WebIM.config.appkey
  269. });
  270. });
  271. }
  272. }
  273. const getIMToken = (isUpdatToken) => {
  274. var user = getStorage('userInfo');
  275. getRequest('/api/v3/im/em/userToken', {}, suc => {
  276. setStorage('imToken', suc.token);
  277. uni.setStorage({
  278. key: "myUsername",
  279. data: 'RS_' + suc.id
  280. });
  281. if (isUpdatToken) {
  282. //更新token
  283. getApp().globalData.conn.open({
  284. user: 'RS_' + suc.id,
  285. accessToken: getStorage('imToken'),
  286. });
  287. getApp().globalData.onLoginSuccess(uni.getStorageSync("myUsername"));
  288. } else {
  289. // initIMChat();
  290. }
  291. }, true);
  292. }
  293. /**
  294. * 跳转进入消息列表
  295. */
  296. const jumpConversation = () => {
  297. if (!isLogins(true)) { //未登录
  298. return;
  299. }
  300. getIMToken(true);
  301. }
  302. /**
  303. * 跳入聊天
  304. * @param {Object} userId 对方用户ID
  305. * @param {Object} avatar 对方用户头像
  306. * @param {Object} realName 对方用户名称
  307. */
  308. const jumpSingleChatRoom = function(userId, avatar, realName) {
  309. getRequest('/manage/v3/im/em/userId', {
  310. id: userId
  311. }, suc => {
  312. var my = uni.getStorageSync("myUsername");
  313. var nameList = {
  314. myName: my,
  315. your: 'rs_' + suc
  316. };
  317. uni.setStorage({
  318. key: 'imUserInfo_' + nameList.your,
  319. data: {
  320. avatar: avatar,
  321. id: userId,
  322. nickName: realName
  323. }
  324. });
  325. uni.navigateTo({
  326. url: "/chat/chatroom/chatroom?username=" + JSON.stringify(nameList)
  327. });
  328. });
  329. }
  330. const getWXUserProfile = (suc) => {
  331. let that = this;
  332. var sessionKey = '';
  333. var openid = '';
  334. getApp().globalData.getCheckSessoin(json => {
  335. sessionKey = json.session_key;
  336. openid = json.openid;
  337. });
  338. try {
  339. uni.getUserProfile({
  340. desc: '完善信息',
  341. success: res => {
  342. updataWxInfos(res.iv, res.encryptedData, sessionKey, openid, suc);
  343. },
  344. fail: res => {
  345. console.log('授权失败数据>>>', res);
  346. msg(res);
  347. }
  348. });
  349. } catch (e) {
  350. // 老版
  351. wx.getUserInfo({
  352. success: function(ress) {
  353. updataWxInfos(res.iv, res.encryptedData, sessionKey, openid, suc);
  354. }
  355. });
  356. }
  357. }
  358. /**
  359. * 保存微信头像
  360. */
  361. const updataWxInfos = (iv, encryptedData, sessionKey, openid, suc) => {
  362. if (!encryptedData || !iv) return false;
  363. let _params = {
  364. sessionKey: sessionKey,
  365. openId: openid,
  366. iv: iv,
  367. encryptedData: encryptedData
  368. };
  369. postRequest('/api/user/weixin/update', _params, json => {
  370. setStorage('userInfo', json);
  371. suc.call();
  372. });
  373. }
  374. const clearValueEmpty = (data) => {
  375. let keyValue = {};
  376. for (let key in data) {
  377. let value = typeof data[key];
  378. if (value == 'string' && value) {
  379. if (data[key] != 'undefined' && data[key] != " " && data[key] != undefined && data[key] !=
  380. null) {
  381. keyValue[key] = data[key];
  382. }
  383. // keyValue[key] = data[key];
  384. } else if (value == 'number' && value != null) {
  385. keyValue[key] = data[key];
  386. } else if (value == 'boolean') {
  387. keyValue[key] = data[key];
  388. } else {
  389. if (data[key]) keyValue[key] = data[key];
  390. }
  391. }
  392. return keyValue;
  393. }
  394. /**
  395. * 公共请求
  396. *
  397. * @param {*} url 请求url
  398. * @param {*} data 请求参数
  399. * @param {*} method 请求方法
  400. * @param {*} success 成功函数
  401. * @param {*} isLoad 是否显示加载提示
  402. */
  403. const baseRequest = (url, data, method, success, isLoad) => {
  404. getToken(token => {
  405. if (isLoad) {
  406. loadIng('加载中');
  407. }
  408. let headers = '';
  409. if (url.indexOf('/v3') != -1) {
  410. headers = getHeader('application/json;charset=UTF-8', '');
  411. } else {
  412. headers = getHeader('application/x-www-form-urlencoded', '');
  413. }
  414. headers.authorityToken = token;
  415. uni.request({
  416. url: env[env.NODE_ENV].apiUrl + url,
  417. data: clearValueEmpty(data),
  418. method: method,
  419. header: headers,
  420. success(json) {
  421. if (isLoad) {
  422. uni.hideLoading();
  423. }
  424. if (json.statusCode !== 200) return msg(json.data.message);
  425. if (json.data.code === 10021 || json.data.code === 10020) {
  426. // console.log("第二次执行")
  427. if(url=='/api/isAuth'){
  428. success.call(this, json.data.data);
  429. return false
  430. }else{
  431. removeStorage('AUTH_TOKEN');
  432. return redirectIndex();
  433. }
  434. }
  435. if (json.data.code !== 0) {
  436. return msg(json.data.msg)
  437. }
  438. // console.log("第二次执行")
  439. let data = json.data.data;
  440. if (typeof data === 'string' && data.indexOf("{") === 0) {
  441. data = JSON.parse(data);
  442. }
  443. success.call(this, data);
  444. },
  445. fail() {
  446. if (isLoad) {
  447. uni.hideLoading();
  448. }
  449. },
  450. // complete(){
  451. // success.call(this, data);
  452. // },
  453. })
  454. })
  455. }
  456. const loadIng = (msg) => {
  457. uni.showLoading({
  458. title: msg,
  459. mask: true
  460. })
  461. }
  462. /**POST请求 */
  463. const postRequest = (url, data, success, isLoad) => {
  464. header['content-type'] = 'application/x-www-form-urlencoded'
  465. baseRequest(url, data, 'post', success, isLoad);
  466. }
  467. const postRequestLoding = (url, data, success, isLoding) => {
  468. header['content-type'] = 'application/x-www-form-urlencoded'
  469. baseRequest(url, data, 'post', success, false, isLoding);
  470. }
  471. /**GET请求 */
  472. const getRequest = (url, data, success, isLoad) => {
  473. header['content-type'] = 'application/x-www-form-urlencoded'
  474. baseRequest(url, data, 'get', success, isLoad);
  475. }
  476. /**Put请求 */
  477. const putRequest = (url, data, success, isLoad) => {
  478. header['content-type'] = 'application/x-www-form-urlencoded'
  479. baseRequest(url, data, 'put', success, isLoad)
  480. }
  481. const putRequestJson = (url, data, success, isLoad) => {
  482. header['content-type'] = 'application/json;charset=UTF-8'
  483. baseRequest(url, data, 'put', success, isLoad)
  484. }
  485. /**public请求 */
  486. const getRequestKDX = (url, data, success, isLoad) => {
  487. baseRequestKDX(url, data, 'get', success, isLoad, publics.appId);
  488. }
  489. /**KDXPOST请求 */
  490. const postRequestKDX = (url, data, success, isLoad) => {
  491. baseRequestKDX(url, data, 'post', success, isLoad, publics.appId);
  492. }
  493. /**
  494. * 公共请求
  495. *
  496. * @param {*} url 请求url
  497. * @param {*} data 请求参数
  498. * @param {*} method 请求方法
  499. * @param {*} success 成功函数
  500. * @param {*} isLoad 是否显示加载提示
  501. */
  502. const baseRequestKDX = (url, data, method, success, isLoad, appId) => {
  503. getToken(token => {
  504. if (isLoad) {
  505. uni.showLoading({
  506. title: '加载中',
  507. mask: true
  508. })
  509. }
  510. var headerKDX = {};
  511. headerKDX.authorityToken = token;
  512. headerKDX.appId = appId;
  513. if (url.indexOf('/v3') != -1) {
  514. headerKDX['content-type'] = 'application/json;charset=UTF-8'
  515. } else {
  516. headerKDX['content-type'] = 'application/x-www-form-urlencoded'
  517. }
  518. console.log('headerKDX==', headerKDX)
  519. uni.request({
  520. url: env[env.NODE_ENV].apiUrl + url,
  521. data: clearValueEmpty(data),
  522. method: method,
  523. header: headerKDX,
  524. success(json) {
  525. if (!isLoad) uni.hideNavigationBarLoading();
  526. uni.hideLoading();
  527. if (json.statusCode !== 200) return msg(json.data.message);
  528. if (json.data.code === 10021 || json.data.code === 10020) {
  529. if (toLogin) {
  530. toLogin = false;
  531. console.log("执行登录")
  532. return redirectIndex();
  533. } else {
  534. console.log("不执行登录")
  535. return;
  536. }
  537. }
  538. toLogin = true;
  539. if (json.data.code !== 0) {
  540. return msg(json.data.msg)
  541. }
  542. let data = json.data.data;
  543. if (typeof data === 'string' && data.indexOf("{") === 0) {
  544. data = JSON.parse(data);
  545. }
  546. success.call(this, data);
  547. },
  548. fail() {
  549. if (!isLoad) uni.hideNavigationBarLoading();
  550. uni.hideLoading();
  551. }
  552. })
  553. }, appId, true)
  554. }
  555. /**
  556. * 上传文件
  557. * @param {*} url 请求url
  558. * @param {*} data 携带数据
  559. * @param {*} success 请求成功函数
  560. */
  561. const uploadFile = (url, data, success) => {
  562. // console.log(env[env.NODE_ENV].apiUrl + url, data)
  563. getToken(token => {
  564. uni.uploadFile({
  565. url: env[env.NODE_ENV].apiUrl + url,
  566. filePath: data,
  567. name: 'file',
  568. header: {
  569. 'appId': getStorage('appId') ? getStorage('appId') : header['appId'],
  570. 'authorityToken': token
  571. },
  572. success(res) {
  573. if (res.statusCode !== 200) return msg('文件上传失败');
  574. let data = JSON.parse(res.data);
  575. if (data.code !== 0) return msg(data.msg);
  576. success.call(this, data.data);
  577. },
  578. })
  579. })
  580. }
  581. /**弹窗 */
  582. const msg = (title, success) => {
  583. if (title) {
  584. uni.showToast({
  585. title: title,
  586. icon: 'none',
  587. success() {
  588. if (success) success.call(this);
  589. }
  590. });
  591. }
  592. return false;
  593. }
  594. const msgConfirm = (msg, success, cancel) => {
  595. uni.showModal({
  596. title: '提示',
  597. content: msg,
  598. success(res) {
  599. if (res.confirm) {
  600. success.call(this);
  601. } else {
  602. if (cancel) cancel.call(this);
  603. }
  604. }
  605. })
  606. }
  607. const msgConfirmText = (msg, confirmText, success, cancel) => {
  608. uni.showModal({
  609. title: '提示',
  610. content: msg,
  611. confirmText: confirmText,
  612. success(res) {
  613. if (res.confirm) {
  614. success.call(this);
  615. } else {
  616. if (cancel) cancel.call(this);
  617. }
  618. }
  619. })
  620. }
  621. const alertMsg = (msg, success) => {
  622. uni.showModal({
  623. title: '提示',
  624. content: msg,
  625. showCancel: false,
  626. success(res) {
  627. if (success) success.call(this);
  628. }
  629. })
  630. return false;
  631. }
  632. const load = msg => {
  633. uni.showLoading({
  634. title: msg,
  635. mask: true
  636. })
  637. }
  638. const g = (url, success, isLoad) => {
  639. getRequest(url, {}, success, isLoad)
  640. }
  641. const p = (url, success, isLoad) => {
  642. postRequest(url, {}, success, isLoad)
  643. }
  644. const send = (url, mobile, success, error) => {
  645. if (!mobile) {
  646. return msg('手机号码不能为空', error);
  647. }
  648. postRequest(url, {
  649. phone: mobile
  650. }, json => {
  651. if (json.code !== 0) {
  652. return msg(json.msg, error);
  653. }
  654. let time = 60;
  655. const initTime = setInterval(() => {
  656. time--;
  657. if (time > 0) {
  658. success.call(this, time + '秒后获取');
  659. } else {
  660. clearInterval(initTime);
  661. success.call(this, '获取验证码');
  662. }
  663. }, 1000)
  664. })
  665. }
  666. // 登陆拦截
  667. const redirectIndex = (params) => {
  668. // uni.clearStorageSync();
  669. removeStorage('userInfo');
  670. // console.log(params)
  671. var pages = getCurrentPages();
  672. // console.log("pages》》》》》登陆拦截", pages);
  673. let currentUrl;
  674. if (pages) {
  675. var currentPage = pages[pages.length - 1];
  676. if (currentPage) {
  677. currentUrl = currentPage.route;
  678. // let query = currentPage.data.query;
  679. // if (query) {
  680. // for (let key in query) {
  681. // const fo = key + '=' + query[key];
  682. // currentUrl += currentUrl.indexOf('?') > -1 ? '&' + fo : '?' + fo;
  683. // }
  684. // }
  685. if (currentUrl) {
  686. console.log('currentUrl》》》》》', currentUrl);
  687. setStorage("REDIRECT_URL", '/' + currentUrl);
  688. // #ifndef MP-WEIXIN
  689. if (currentUrl == 'pages/applogin/index') {
  690. console.log('当前已经处于登录页面,不在向下执行跳转');
  691. return
  692. }
  693. // #endif
  694. // #ifdef MP-WEIXIN
  695. if (currentUrl == 'pages/authorize/authorize') {
  696. console.log('当前已经处于登录页面,不在向下执行跳转');
  697. return
  698. }
  699. // #endif
  700. }
  701. }
  702. }
  703. var redirectUrl = '';
  704. //小程序环境设置
  705. var ua = window.navigator.userAgent.toLowerCase();
  706. //判断是否是微信环境
  707. if (ua.match(/MicroMessenger/i) == 'micromessenger') {
  708. jWeixin.miniProgram.getEnv((res) => {
  709. if(res.miniprogram){
  710. redirectUrl = '/pages/authorize/authorize' + (params ? params : '');
  711. }else{
  712. redirectUrl = '/pages/applogin/index' + (params ? params : '');
  713. }
  714. })
  715. }else{
  716. redirectUrl = '/pages/applogin/index' + (params ? params : '');
  717. }
  718. var jump = getStorage('authorize_jump') //以下解决多次跳转登录页的重点
  719. if (!jump || currentUrl !== 'pages/authorize/authorize') {
  720. //以下做token失效的操作
  721. setTimeout(() => {
  722. // uni.navigateTo({
  723. // url: redirectUrl
  724. // })
  725. h5util.jumpUrlExternal({
  726. url:redirectUrl,
  727. onlyWeb:false
  728. })
  729. }, 100)
  730. setStorage('authorize_jump', 'true')
  731. }
  732. return false;
  733. }
  734. const saveImage = (url) => {
  735. uni.saveImageToPhotosAlbum({
  736. filePath: url,
  737. success() {
  738. msg('图片保存成功');
  739. }
  740. })
  741. }
  742. const saveImageToPhotosAlbum = (url) => {
  743. if (!url) return msg('小程序码不存在');
  744. uni.getImageInfo({
  745. src: url,
  746. success(json) {
  747. uni.getSetting({
  748. success(res) {
  749. if (!res.authSetting['scope.writePhotosAlbum']) {
  750. uni.authorize({
  751. scope: 'scope.writePhotosAlbum',
  752. success() {
  753. saveImage(json.path);
  754. }
  755. })
  756. } else {
  757. saveImage(json.path);
  758. }
  759. }
  760. })
  761. }
  762. })
  763. }
  764. const getConfig = (config, id) => {
  765. let con = {};
  766. config.info.forEach(inf => {
  767. // console.log(inf.version, inf.version ? inf.version.indexOf(id) : '-')
  768. if (inf.isSet && (!inf.version || inf.version.indexOf(id) > -1))
  769. con[inf.setName] = inf.setDefault;
  770. });
  771. config.card.forEach(inf => {
  772. if (inf.isSet && (!inf.version || inf.version.indexOf(id) > -1))
  773. con[inf.setName] = inf.setDefault;
  774. });
  775. config.realTime.forEach(inf => {
  776. if (inf.isSet && (!inf.version || inf.version.indexOf(id) > -1))
  777. con[inf.setName] = inf.setDefault;
  778. });
  779. return con;
  780. }
  781. const isLogin = async (error) => {
  782. let userInfo = getStorage('userInfo');
  783. if (!userInfo || !userInfo.id) {
  784. if (error) return false;
  785. // console.log("第一次执行")
  786. return redirectIndex();
  787. }
  788. return true;
  789. }
  790. //检测是否登录
  791. const isLogins = async(isGoLogin) => {
  792. let userInfo = getStorage('userInfo');
  793. if (!userInfo) {
  794. if (isGoLogin) {
  795. redirectIndex();
  796. }
  797. return false;
  798. }
  799. return true;
  800. }
  801. //检测是否登录接口形式
  802. const isAuthFn = (isGoLogin)=>{
  803. return new Promise((r,j)=>{
  804. postRequest('/api/isAuth',{},res=>{
  805. if(!res){
  806. if (isGoLogin) {
  807. redirectIndex();
  808. }
  809. }
  810. r(res?true:false)
  811. })
  812. })
  813. }
  814. const setStorage = (key, value) => {
  815. uni.setStorageSync(env.NODE_ENV + "_" + key, value)
  816. }
  817. const getStorage = (key) => {
  818. return uni.getStorageSync(env.NODE_ENV + "_" + key)
  819. }
  820. const removeStorage = (key) => {
  821. return uni.removeStorageSync(env.NODE_ENV + "_" + key);
  822. }
  823. /**
  824. * 缓存模板id
  825. */
  826. // const gettmplIds = () => {
  827. // getRequest('/api/wxmes/querylist', {}, res => {
  828. // setStorage('tmplIds', res)
  829. // console.log(res)
  830. // })
  831. // }
  832. //微信支付订单公共方法
  833. const payOrder = (id, success, type) => {
  834. var loctionAddressMap = getStorage('loctionAddressMap');
  835. var datas = {
  836. id: id
  837. };
  838. // #ifdef H5
  839. datas.payChannel = 'wx_mweb';
  840. // #endif
  841. if (loctionAddressMap) {
  842. datas.province = loctionAddressMap.province;
  843. datas.city = loctionAddressMap.city;
  844. };
  845. if (getStorage('isVideoScene')) {
  846. uni.checkBeforeAddOrder({
  847. success(res) {
  848. console.log('下单前置检查', res)
  849. datas.sceneCheckStr = JSON.stringify(res.data);
  850. wexinPay(datas, success, type)
  851. },
  852. })
  853. } else {
  854. wexinPay(datas, success, type)
  855. }
  856. }
  857. //支付中或已支付
  858. const payStatus = (json, id, success) => {
  859. if (json && json.code === 115) {
  860. setTimeout(res => {
  861. uni.hideLoading();
  862. isShowLoading = false;
  863. uni.showModal({
  864. title: '提示',
  865. content: '当前订单正在支付或30秒后重试',
  866. confirmText: '我知道了',
  867. showCancel: false,
  868. success(res) {}
  869. })
  870. }, 3000)
  871. return false;
  872. }
  873. if (json.type === 2 || json.type === 3) {
  874. //调用后台判断订单是否支付成功
  875. let i = 0;
  876. let timer = setInterval(() => {
  877. postRequest('/api/order/check', {
  878. id: id
  879. }, res => {
  880. if (res.isSuccess || i === 4) {
  881. clearInterval(timer);
  882. success.call(this, res.isSuccess);
  883. } else {
  884. i++;
  885. }
  886. }, true)
  887. }, 200);
  888. // success.call(this);
  889. return false;
  890. }
  891. }
  892. // 支付成功回调
  893. const paySuccess = (id, success) => {
  894. console.log('支付成功')
  895. let i = 0;
  896. let timer = setInterval(() => {
  897. postRequest('/api/order/check', {
  898. id: id
  899. }, res => {
  900. console.log(i)
  901. if (res.isSuccess || i === 5) {
  902. clearInterval(timer);
  903. success.call(this, res.isSuccess);
  904. } else {
  905. i++;
  906. }
  907. }, true)
  908. }, 1000);
  909. }
  910. // 支付失败回调
  911. const payFail = (id, type) => {
  912. console.log('支付失败')
  913. postRequest('/api/order/cancelPay', {
  914. id: id
  915. }, data => {
  916. if (type == 'payment') return false;
  917. uni.redirectTo({
  918. url: '/mine/orderDet/orderDet?id=' + id,
  919. })
  920. })
  921. }
  922. //微信支付订单公共方法 ==活动
  923. const payOrders = (id, success, type) => {
  924. let isShowLoading = false;
  925. if (!isShowLoading) {
  926. loadIng('加载中');
  927. // console.log('加载中')
  928. isShowLoading = true;
  929. }
  930. postRequest('/api/order/weixin/pay', {
  931. id: id
  932. }, json => {
  933. // console.log(json)
  934. payStatus(json, id, success)
  935. uni.requestPayment({
  936. timeStamp: json.timeStamp,
  937. nonceStr: json.nonceStr,
  938. package: json.packages,
  939. signType: json.signType,
  940. paySign: json.sign,
  941. success: function() {
  942. paySuccess(id, success)
  943. },
  944. fail: function(res) {
  945. console.log('支付失败')
  946. postRequest('/api/order/cancelPay', {
  947. id: id
  948. }, data => {
  949. if (type == 'registered') {
  950. uni.redirectTo({
  951. url: '/onlineregistration/detail/detail?id=' + id,
  952. })
  953. } else {
  954. uni.redirectTo({
  955. url: '/match/activitys/activity',
  956. })
  957. }
  958. })
  959. console.log(res);
  960. }
  961. })
  962. if (isShowLoading) {
  963. uni.hideLoading();
  964. isShowLoading = false;
  965. }
  966. })
  967. }
  968. const wexinPay = (datas, success, type) => {
  969. let isShowLoading = false;
  970. if (!isShowLoading) {
  971. loadIng('加载中');
  972. // console.log('加载中')
  973. isShowLoading = true;
  974. }
  975. // #ifdef MP-WEIXIN
  976. postRequest('/api/order/weixin/pay', datas, json => {
  977. // console.log('json',json)
  978. payStatus(json, datas.id, success, type)
  979. if (getStorage('isVideoScene')) {
  980. console.log('拉起收银台')
  981. wx.requestOrderPayment({
  982. timeStamp: json.timeStamp,
  983. nonceStr: json.nonceStr,
  984. package: json.packages,
  985. signType: json.signType,
  986. orderInfo: json.orderInfo,
  987. paySign: json.sign,
  988. success: function() {
  989. paySuccess(datas.id, success)
  990. },
  991. fail: function(res) {
  992. console.log(res);
  993. payFail(datas.id, type)
  994. }
  995. })
  996. } else {
  997. uni.requestPayment({
  998. timeStamp: json.timeStamp,
  999. nonceStr: json.nonceStr,
  1000. package: json.packages,
  1001. signType: json.signType,
  1002. paySign: json.sign,
  1003. success: function() {
  1004. paySuccess(datas.id, success)
  1005. },
  1006. fail: function(res) {
  1007. console.log(res);
  1008. payFail(datas.id, type)
  1009. }
  1010. })
  1011. }
  1012. if (isShowLoading) {
  1013. uni.hideLoading();
  1014. isShowLoading = false;
  1015. }
  1016. })
  1017. // #endif
  1018. // #ifdef MP-ALIPAY
  1019. datas.payChannel = 'alipay_lite ';
  1020. postRequest('/api/order/weixin/pay', datas, json => {
  1021. // console.log(json)
  1022. if (json.type === 2 || json.type === 3) {
  1023. //调用后台判断订单是否支付成功
  1024. let i = 0;
  1025. let timer = setInterval(() => {
  1026. postRequest('/api/order/check', {
  1027. id: id
  1028. }, res => {
  1029. if (res.isSuccess || i === 4) {
  1030. clearInterval(timer);
  1031. success.call(this, res.isSuccess);
  1032. } else {
  1033. i++;
  1034. }
  1035. }, true)
  1036. }, 200);
  1037. // success.call(this);
  1038. return false;
  1039. }
  1040. uni.requestPayment({
  1041. provider: 'alipay',
  1042. orderInfo: json.packages,
  1043. success: function() {
  1044. paySuccess(datas.id, success)
  1045. },
  1046. fail: function(res) {
  1047. payFail(datas.id)
  1048. console.log(res);
  1049. }
  1050. })
  1051. if (isShowLoading) {
  1052. uni.hideLoading();
  1053. isShowLoading = false;
  1054. }
  1055. })
  1056. // #endif
  1057. // #ifdef H5
  1058. postRequest('/api/order/weixin/pay', datas, json => {
  1059. // console.log(json)
  1060. if (json.type === 2 || json.type === 3) {
  1061. //调用后台判断订单是否支付成功
  1062. paySuccess(datas.id, success);
  1063. // success.call(this);
  1064. return false;
  1065. }
  1066. if (json.channel === 9 || json.channel === 2) {
  1067. //调用后台判断订单是否支付成功
  1068. window.location.href = json.payUrl;
  1069. return false;
  1070. }
  1071. if (isShowLoading) {
  1072. uni.hideLoading();
  1073. isShowLoading = false;
  1074. }
  1075. })
  1076. // #endif
  1077. }
  1078. //微信支付订单公共方法
  1079. const payOrdersss = (id, success) => {
  1080. let isShowLoading = false;
  1081. if (!isShowLoading) {
  1082. loadIng('加载中');
  1083. // console.log('加载中')
  1084. isShowLoading = true;
  1085. }
  1086. var loctionAddressMap = getStorage('loctionAddressMap');
  1087. var datas = {
  1088. id: id
  1089. };
  1090. if (loctionAddressMap) {
  1091. datas.province = loctionAddressMap.province;
  1092. datas.city = loctionAddressMap.city;
  1093. };
  1094. // #ifdef MP-WEIXIN
  1095. postRequest('/api/order/weixin/pay', datas, json => {
  1096. // console.log(json)
  1097. if (json.type === 2 || json.type === 3) {
  1098. //调用后台判断订单是否支付成功
  1099. let i = 0;
  1100. let timer = setInterval(() => {
  1101. postRequest('/api/order/check', {
  1102. id: id
  1103. }, res => {
  1104. if (res.isSuccess || i === 4) {
  1105. clearInterval(timer);
  1106. success.call(this, res.isSuccess);
  1107. } else {
  1108. i++;
  1109. }
  1110. }, true)
  1111. }, 200);
  1112. // success.call(this);
  1113. return false;
  1114. }
  1115. uni.requestPayment({
  1116. timeStamp: json.timeStamp,
  1117. nonceStr: json.nonceStr,
  1118. package: json.packages,
  1119. signType: json.signType,
  1120. paySign: json.sign,
  1121. success: function() {
  1122. console.log('支付成功')
  1123. let i = 0;
  1124. let timer = setInterval(() => {
  1125. postRequest('/api/order/check', {
  1126. id: id
  1127. }, res => {
  1128. console.log(i)
  1129. if (res.isSuccess || i === 5) {
  1130. clearInterval(timer);
  1131. success.call(this, res.isSuccess);
  1132. } else {
  1133. i++;
  1134. }
  1135. }, true)
  1136. }, 500);
  1137. },
  1138. fail: function(res) {
  1139. console.log('支付失败')
  1140. postRequest('/api/order/cancelPay', {
  1141. id: id
  1142. }, data => {
  1143. uni.redirectTo({
  1144. url: '/mine/orderDet/orderDet?id=' + id,
  1145. })
  1146. })
  1147. console.log(res);
  1148. }
  1149. })
  1150. if (isShowLoading) {
  1151. uni.hideLoading();
  1152. isShowLoading = false;
  1153. }
  1154. })
  1155. // #endif
  1156. // #ifdef MP-ALIPAY
  1157. datas.payChannel = 'alipay_lite ';
  1158. postRequest('/api/order/weixin/pay', datas, json => {
  1159. // console.log(json)
  1160. if (json.type === 2 || json.type === 3) {
  1161. //调用后台判断订单是否支付成功
  1162. let i = 0;
  1163. let timer = setInterval(() => {
  1164. postRequest('/api/order/check', {
  1165. id: id
  1166. }, res => {
  1167. if (res.isSuccess || i === 4) {
  1168. clearInterval(timer);
  1169. success.call(this, res.isSuccess);
  1170. } else {
  1171. i++;
  1172. }
  1173. }, true)
  1174. }, 200);
  1175. // success.call(this);
  1176. return false;
  1177. }
  1178. uni.requestPayment({
  1179. provider: 'alipay',
  1180. orderInfo: json.packages,
  1181. success: function() {
  1182. console.log('支付成功')
  1183. let i = 0;
  1184. let timer = setInterval(() => {
  1185. postRequest('/api/order/check', {
  1186. id: id
  1187. }, res => {
  1188. console.log(i)
  1189. if (res.isSuccess || i === 5) {
  1190. clearInterval(timer);
  1191. success.call(this, res.isSuccess);
  1192. } else {
  1193. i++;
  1194. }
  1195. }, true)
  1196. }, 500);
  1197. },
  1198. fail: function(res) {
  1199. console.log('支付失败')
  1200. postRequest('/api/order/cancelPay', {
  1201. id: id
  1202. }, data => {
  1203. uni.redirectTo({
  1204. url: '/mine/orderDet/orderDet?id=' + id,
  1205. })
  1206. })
  1207. console.log(res);
  1208. }
  1209. })
  1210. if (isShowLoading) {
  1211. uni.hideLoading();
  1212. isShowLoading = false;
  1213. }
  1214. })
  1215. // #endif
  1216. }
  1217. /**
  1218. * type 4为余额充值
  1219. */
  1220. const payAppOrder = (id, openId, type, success) => {
  1221. let isShowLoading = false;
  1222. if (!isShowLoading) {
  1223. loadIng('加载中');
  1224. isShowLoading = true;
  1225. }
  1226. var loctionAddressMap = getStorage('loctionAddressMap');
  1227. var datas = {
  1228. id: id,
  1229. openId: openId
  1230. };
  1231. if (loctionAddressMap) {
  1232. datas.province = loctionAddressMap.province;
  1233. datas.city = loctionAddressMap.city;
  1234. };
  1235. var url = '';
  1236. if (type == 4) {
  1237. url = '/api/members/v2/weixin/pay';
  1238. datas.status = type;
  1239. } else {
  1240. url = '/api/order/v2/weixin/pay';
  1241. }
  1242. postRequest(url, datas, json => {
  1243. var urlapi = '';
  1244. var dataP = {};
  1245. if (type == 4) {
  1246. urlapi = '/api/rrLogs/check'
  1247. dataP.logsId = id;
  1248. } else {
  1249. urlapi = '/api/order/v2/check'
  1250. dataP.id = id;
  1251. }
  1252. if (json.type === 2 || json.type === 3) {
  1253. //调用后台判断订单是否支付成功
  1254. let i = 0;
  1255. let timer = setInterval(() => {
  1256. postRequest(urlapi, dataP, res => {
  1257. if (res.isSuccess || i === 4) {
  1258. clearInterval(timer);
  1259. success.call(this, res.isSuccess);
  1260. } else {
  1261. i++;
  1262. }
  1263. }, true)
  1264. }, 200);
  1265. return false;
  1266. }
  1267. uni.requestPayment({
  1268. timeStamp: json.timeStamp,
  1269. nonceStr: json.nonceStr,
  1270. package: json.packages,
  1271. signType: json.signType,
  1272. paySign: json.sign,
  1273. success: function() {
  1274. let i = 0;
  1275. loadIng('加载中');
  1276. let timer = setInterval(() => {
  1277. postRequest(urlapi, dataP, res => {
  1278. console.log(i)
  1279. if (res.isSuccess || i === 5) {
  1280. clearInterval(timer);
  1281. uni.hideLoading();
  1282. success.call(this, res.isSuccess);
  1283. } else {
  1284. i++;
  1285. }
  1286. })
  1287. }, 500);
  1288. },
  1289. fail: function(res) {
  1290. success.call(this, false);
  1291. msg('支付失败')
  1292. }
  1293. })
  1294. if (isShowLoading) {
  1295. uni.hideLoading();
  1296. isShowLoading = false;
  1297. }
  1298. });
  1299. }
  1300. const receipt = (id, success) => {
  1301. let isShowLoading = false;
  1302. if (!isShowLoading) {
  1303. loadIng('加载中');
  1304. // console.log('加载中')
  1305. isShowLoading = true;
  1306. }
  1307. postRequest('/api/receipt/weixin/pay', {
  1308. id: id
  1309. }, json => {
  1310. // console.log(json)
  1311. if (json.type === 2) {
  1312. //调用后台判断订单是否支付成功
  1313. let i = 0;
  1314. let timer = setInterval(() => {
  1315. postRequest('/api/receipt/check', {
  1316. receiptId: id
  1317. }, res => {
  1318. if (res.isSuccess || i === 4) {
  1319. clearInterval(timer);
  1320. success.call(this, res.isSuccess);
  1321. } else {
  1322. i++;
  1323. }
  1324. }, true)
  1325. }, 200);
  1326. // success.call(this);
  1327. return false;
  1328. }
  1329. uni.requestPayment({
  1330. timeStamp: json.timeStamp,
  1331. nonceStr: json.nonceStr,
  1332. package: json.packages,
  1333. signType: json.signType,
  1334. paySign: json.sign,
  1335. success: function() {
  1336. console.log('支付成功')
  1337. let i = 0;
  1338. let timer = setInterval(() => {
  1339. postRequest('/api/receipt/check', {
  1340. receiptId: id
  1341. }, res => {
  1342. console.log(i)
  1343. if (res.isSuccess || i === 5) {
  1344. clearInterval(timer);
  1345. success.call(this, res.isSuccess);
  1346. } else {
  1347. i++;
  1348. }
  1349. }, true)
  1350. }, 500);
  1351. },
  1352. fail: function(res) {
  1353. console.log('支付失败')
  1354. }
  1355. })
  1356. if (isShowLoading) {
  1357. uni.hideLoading();
  1358. isShowLoading = false;
  1359. }
  1360. })
  1361. }
  1362. const authSetting = (authority, success, error) => {
  1363. // console.log(authority,success)
  1364. // #ifndef H5
  1365. uni.getSetting({
  1366. success(res) {
  1367. if (res.authSetting[authority]) {
  1368. success.call(this);
  1369. return false;
  1370. }
  1371. uni.authorize({
  1372. scope: authority,
  1373. success() {
  1374. success.call(this);
  1375. },
  1376. fail: function(res) {
  1377. error.call(this);
  1378. }
  1379. })
  1380. }
  1381. })
  1382. // #endif
  1383. }
  1384. const getLocation = (suss) => {
  1385. // #ifdef MP-WEIXIN
  1386. authSetting('scope.userLocation', () => {
  1387. // load('定位中…');
  1388. uni.getLocation({
  1389. type: 'gcj02',
  1390. // isHighAccuracy: true,
  1391. success: function(res) {
  1392. // uni.hideLoading();
  1393. console.log(res, "789")
  1394. suss.call(this, res);
  1395. },
  1396. fail: function(res) {
  1397. console.log("调用失败", res)
  1398. suss.call(this, 2)
  1399. // uni.hideLoading();
  1400. }
  1401. })
  1402. }, () => {
  1403. // uni.hideLoading();
  1404. console.log("取消")
  1405. suss.call(this, 1)
  1406. });
  1407. // #endif
  1408. // #ifdef MP-ALIPAY
  1409. uni.getLocation({
  1410. type: 'gcj02',
  1411. // isHighAccuracy: true,
  1412. success: function(res) {
  1413. // uni.hideLoading();
  1414. console.log(res, "789")
  1415. suss.call(this, res);
  1416. },
  1417. fail: function(res) {
  1418. console.log("调用失败", res)
  1419. suss.call(this, 2)
  1420. // uni.hideLoading();
  1421. }
  1422. })
  1423. // #endif
  1424. // #ifdef H5
  1425. uni.getLocation({
  1426. type: 'gcj02',
  1427. isHighAccuracy: true,
  1428. success: function(res) {
  1429. // uni.hideLoading();
  1430. console.log(res)
  1431. suss.call(this, res, "789");
  1432. },
  1433. fail: function(res) {
  1434. console.log("调用失败", res)
  1435. suss.call(this, 2)
  1436. // uni.hideLoading();
  1437. }
  1438. })
  1439. // #endif
  1440. }
  1441. const scopeAddress = (success) => {
  1442. // #ifdef MP-WEIXIN
  1443. authSetting('scope.address', () => {
  1444. uni.chooseAddress({
  1445. success: function(res) {
  1446. success.call(this, res);
  1447. },
  1448. })
  1449. }, () => {
  1450. msg('未设置开放权限')
  1451. });
  1452. // #endif
  1453. // #ifdef MP-ALIPAY
  1454. uni.chooseAddress({
  1455. success: function(res) {
  1456. success.call(this, res);
  1457. },
  1458. })
  1459. // #endif
  1460. }
  1461. const isAuth = () => {
  1462. const user = getStorage('userInfo');
  1463. return user && user.id;
  1464. }
  1465. // 去掉字符串中的特殊字符和转义字符
  1466. const excludeSpecial = (s) => {
  1467. // 去掉转义字符
  1468. // s = s.replace(/[\'\"\\\/\b\f\n\r\t]/g, '');
  1469. const pattern = /[`~!@#$^&*()=|{}':;',\\\[\]\.<>\/?~!@#¥……&*()——|{}【】';:""' + - - _ % 。,、?\s]/g;
  1470. if (s != undefined || s != null) {
  1471. s = s.replace(pattern, "")
  1472. }
  1473. return s;
  1474. }
  1475. // 行为操作
  1476. const saveBehavior = (params, success) => {
  1477. //用户行为:behavior 1、关注 2、收藏 3、点赞 4、浏览 5、确认 6、分享
  1478. //用户行为操作对象:type 1、产品 2、赛事 3、未知 4、内容 5、课程 6、老师 7、素材 8、题目 9、资料领取 10、招聘职位 11、用户 12、用户须知
  1479. //上级浏览ID:parentId
  1480. //来源用户:pid
  1481. postRequest('/api/v3/save', params, data => {
  1482. success.call(this, data)
  1483. })
  1484. }
  1485. const getBehavior = (params, success) => {
  1486. //用户行为:behavior 1、关注 2、收藏 3、点赞 4、浏览 5、确认 6、分享
  1487. //用户行为操作对象:type 1、产品 2、赛事 3、未知 4、内容 5、课程 6、老师 7、素材 8、题目 9、资料领取 10、招聘职位 11、用户 12、用户须知
  1488. getRequest('/api/v3/get', params, data => {
  1489. success.call(this, data)
  1490. })
  1491. }
  1492. const getBehaviorCount = (params, success) => {
  1493. //用户行为:behavior 1、关注 2、收藏 3、点赞 4、浏览 5、确认
  1494. //用户行为操作对象:type 1、产品 2、赛事 3、未知 4、内容 5、课程 6、老师 7、素材 8、题目 9、资料领取 10、招聘职位 11、用户 12、用户须知
  1495. getRequest('/api/v3/behavior/count', params, data => {
  1496. success.call(this, data)
  1497. })
  1498. }
  1499. const getBehaviorUpdate = (params, success) => {
  1500. //主键id:save接口返回的id
  1501. postRequest('/api/v3/update', params, data => {
  1502. success.call(this, data)
  1503. })
  1504. }
  1505. const isTabPage = (pageName, solution_name) => {
  1506. console.log('isTabPage', pageName)
  1507. // pageName 当前页面channel solution_name 线上配置name
  1508. let pass = false;
  1509. if (pageName) {
  1510. for (let i = 0; i < tab.length; i++) {
  1511. let it = tab[i];
  1512. if (solution_name && solution_name != ' ') {
  1513. if (it.name == solution_name) {
  1514. let tlist = it.list.map(iit => {
  1515. return iit.channel
  1516. })
  1517. if (tlist.indexOf(pageName) != -1) return true
  1518. }
  1519. } else {
  1520. if (it.name == 'default') {
  1521. let tlist = it.list.map(iit => {
  1522. return iit.channel
  1523. })
  1524. if (tlist.indexOf(pageName) != -1) return true
  1525. }
  1526. }
  1527. }
  1528. }
  1529. return pass
  1530. }
  1531. const toTabBar = (pageName, options = {}) => {
  1532. console.log('toTabBar', pageName)
  1533. // let url = '/pages/tabBar/index?pageChannel='+pageName
  1534. // if(Object.keys(options).length>0){
  1535. // for (let i in options) {
  1536. // url = url+'&'+i+'='+options[i]
  1537. // }
  1538. // }
  1539. options.pageChannel = pageName
  1540. console.log('toTabBar options', options)
  1541. setStorage('optionsPageDate', options)
  1542. uni.switchTab({
  1543. url: '/pages/tabBar/index'
  1544. })
  1545. }
  1546. /**
  1547. * 记录用户端口和来源
  1548. * @param port 用户端口(不需要传) 1.小程序(默认) 2.微信好友 3.微信群 4.企微好友 5.企微群 6.H5 7.公众号 8.导入 9.APP
  1549. * @param source 来源 1 小程序搜索 2 活动报名 3 名片分享 4商品分享 5店铺分享 6文章分享 7小程序分享 8素材分享 9公众号分享 10公众号搜索 11企微活码 12微信活码 13手工录入 14 5g消息
  1550. * @param uid 用户id
  1551. */
  1552. const savePortAndSource = ({
  1553. port = 1,
  1554. source = '',
  1555. uid = ''
  1556. } = {}) => {
  1557. // 获取用户端口
  1558. portObj = getUserPort()
  1559. let dataP = {
  1560. portType: portObj.port,
  1561. resourceType: source,
  1562. uid: uid
  1563. };
  1564. // 如果是从好友或群聊端口进入且没有来源则算作 通过小程序分享进入
  1565. if ((dataP.portType == 2 || dataP.portType == 3 || dataP.portType == 4 || dataP.portType == 5) && !
  1566. source) {
  1567. dataP.resourceType = 7
  1568. }
  1569. // 端口有来源则展示端口来源
  1570. if (portObj.resourceType) {
  1571. dataP.resourceType = portObj.resourceType
  1572. }
  1573. // 默认为主动进入小程序用户,H5 APP暂时未开放,如需统计主动进入或分享H5 APP等数据需要再讨论用户来源类型
  1574. if (!dataP.resourceType) dataP.resourceType = 1
  1575. console.log('调用用户端口和来源保存接口:', dataP)
  1576. postRequest('/api/v3/application/behavior/saveUserResourcePort', dataP, data => {
  1577. });
  1578. }
  1579. const getWebchatAppId = () => {
  1580. // #ifdef H5
  1581. if (utils.isWeixin()) {
  1582. getRequest(api.webchat_appId, {}, data => {
  1583. if (data) {
  1584. setStorage('webchatAppId', data);
  1585. }
  1586. });
  1587. }
  1588. // #endif
  1589. }
  1590. const getwxConfig = (suc) => {
  1591. getRequest('/api/config', {}, data => {
  1592. setStorage('configRes', JSON.stringify(data));
  1593. getApp().globalData.upColor(true)
  1594. suc.call(this, data);
  1595. });
  1596. }
  1597. // 获取用户端口
  1598. const getUserPort = () => {
  1599. const sinfo = uni.getSystemInfoSync();
  1600. let port = 1
  1601. let resourceType = null
  1602. // #ifdef H5
  1603. console.log("当前端口: H5端")
  1604. port = 6
  1605. // #endif
  1606. // #ifdef APP
  1607. console.log("当前端口: APP端")
  1608. port = 9
  1609. // #endif
  1610. // #ifdef MP-WEIXIN
  1611. console.log("当前端口: 小程序端")
  1612. port = 1
  1613. let params = uni.getEnterOptionsSync()
  1614. console.log("小程序端参数: ", params)
  1615. switch (params.scene) {
  1616. case 1007: { //单人聊天会话中的小程序消息卡片
  1617. port = 2
  1618. if (sinfo.environment == 'wxwork') {
  1619. port = 4
  1620. }
  1621. }
  1622. break;
  1623. case 1008: { //群聊会话中的小程序消息卡片
  1624. port = 3
  1625. console.log("小程序群场景值1008")
  1626. if (sinfo.environment == 'wxwork') {
  1627. console.log("小程序群场景值1008 企微环境")
  1628. port = 5
  1629. }
  1630. try {
  1631. uni.getGroupEnterInfo({
  1632. success: (res) => {
  1633. console.log("getGroupEnterInfo 小程序群聊id:", res)
  1634. // if (res.encryptedData && res.iv) {
  1635. // app.globalData.getCheckSessoin(json => {
  1636. // let _params = {
  1637. // sessionKey: json.sessionKey,
  1638. // openId: json.openid,
  1639. // iv: res.iv,
  1640. // encryptedData: res.encryptedData
  1641. // };
  1642. // postRequest('', _params, json => {
  1643. // });
  1644. // });
  1645. // }
  1646. },
  1647. fail: (error) => {
  1648. console.log("小程序群聊id error:", error)
  1649. },
  1650. })
  1651. } catch (e) {
  1652. //TODO handle the exception
  1653. }
  1654. }
  1655. break;
  1656. case 1044: { //带 shareTicket 的小程序消息卡片(群聊)
  1657. port = 3
  1658. if (sinfo.environment == 'wxwork') {
  1659. port = 5
  1660. }
  1661. console.log("小程序群聊id:", params.shareTicket)
  1662. try {
  1663. uni.getGroupEnterInfo({
  1664. success: (res) => {
  1665. console.log("小程序群聊id:", res)
  1666. }
  1667. })
  1668. } catch (e) {
  1669. //TODO handle the exception
  1670. }
  1671. }
  1672. break;
  1673. case 1020: { //公众号 profile 页相关小程序列表(已废弃)
  1674. port = 7
  1675. }
  1676. break;
  1677. case 1035: { //公众号自定义菜单
  1678. port = 7
  1679. }
  1680. break;
  1681. case 1043: { //公众号模板消息
  1682. port = 7
  1683. }
  1684. break;
  1685. case 1074: { //公众号会话下发的小程序消息卡片
  1686. port = 7
  1687. }
  1688. break;
  1689. case 1082: { //公众号会话下发的文字链
  1690. port = 7
  1691. }
  1692. break;
  1693. case 1091: { //公众号文章商品卡片
  1694. port = 7
  1695. }
  1696. break;
  1697. case 1065: { //URL scheme 用户5g消息进入小程序
  1698. port = 1
  1699. resourceType = 14
  1700. }
  1701. break;
  1702. default:
  1703. break;
  1704. }
  1705. // #endif
  1706. // 目前判断从企微环境打开的都算企微好友,无发获取到是从群聊还是好友聊天中打开的程序
  1707. if (sinfo.environment == 'wxwork') {
  1708. port = 4
  1709. }
  1710. console.log("进入端口类型: ", port)
  1711. return {
  1712. port: port,
  1713. resourceType: resourceType
  1714. }
  1715. }
  1716. /**
  1717. * text:展示导航名
  1718. * iconPath:未点击显示图标
  1719. * selectedIconPath:选中图标
  1720. * pagePath:页面路径
  1721. * channel:匹配名
  1722. * click:点击事件
  1723. * index:排序
  1724. * show:是否显示
  1725. */
  1726. let config = getStorage('configRes') ? JSON.parse(getStorage('configRes')) : {};
  1727. var tab = [{
  1728. name: 'default',
  1729. list: [{
  1730. text: "首页",
  1731. iconPath: "/static/pages/images/home.png",
  1732. selectedIconPath: '/static/pages/images/' + publics.iconSrc + 'home_h.png',
  1733. pagePath: 'pages/index/index',
  1734. channel: 'home',
  1735. click: 'goHome',
  1736. index: 1,
  1737. show: true
  1738. },
  1739. {
  1740. text: "服务项目",
  1741. iconPath: '/static/pages/images/service.png',
  1742. selectedIconPath: '/static/pages/images/' + publics.iconSrc + 'service_h.png',
  1743. pagePath: 'pages/service/index',
  1744. click: 'goService',
  1745. channel: 'service',
  1746. index: 2,
  1747. show: true
  1748. },
  1749. {
  1750. text: "我的",
  1751. iconPath: '/static/pages/images/user.png',
  1752. selectedIconPath: '/static/pages/images/' + publics.iconSrc + 'user_h.png',
  1753. pagePath: 'pages/user/user',
  1754. click: 'goUser',
  1755. channel: 'user',
  1756. index: 3,
  1757. show: true
  1758. }
  1759. ]
  1760. },
  1761. {
  1762. name: 'taoyanxi',
  1763. list: [{
  1764. text: "首页",
  1765. iconPath: "/static/pages/images/home.png",
  1766. selectedIconPath: '/static/pages/images/' + publics.iconSrc + 'home_h.png',
  1767. iconPathOn: "home.png",
  1768. selectedIconPathOn: 'home_h.png',
  1769. pagePath: 'pages/index/index',
  1770. channel: 'home',
  1771. click: 'goHome',
  1772. index: 1,
  1773. show: true
  1774. },
  1775. {
  1776. text: "产品",
  1777. iconPath: '/static/pages/images/service.png',
  1778. selectedIconPath: '/static/pages/images/' + publics.iconSrc + 'service_h.png',
  1779. iconPathOn: "service.png",
  1780. selectedIconPathOn: 'service_h.png',
  1781. pagePath: 'pages/service/index',
  1782. click: 'goService',
  1783. channel: 'service',
  1784. index: 2,
  1785. show: true
  1786. },
  1787. {
  1788. text: "我的",
  1789. iconPath: '/static/pages/images/user.png',
  1790. selectedIconPath: '/static/pages/images/' + publics.iconSrc + 'user_h.png',
  1791. iconPathOn: "user.png",
  1792. selectedIconPathOn: 'user_h.png',
  1793. pagePath: 'pages/user/user',
  1794. click: 'goUser',
  1795. channel: 'user',
  1796. index: 3,
  1797. show: true
  1798. }
  1799. ]
  1800. },
  1801. {
  1802. name: 'yaofang',
  1803. list: [{
  1804. text: "首页",
  1805. iconPath: "/static/pages/images/home.png",
  1806. selectedIconPath: '/static/pages/images/' + publics.iconSrc + 'home_h.png',
  1807. iconPathOn: "home.png",
  1808. selectedIconPathOn: 'home_h.png',
  1809. pagePath: 'home/index/index_yaofang',
  1810. channel: 'home',
  1811. click: 'goHome',
  1812. index: 1,
  1813. show: true
  1814. },
  1815. {
  1816. text: "分类",
  1817. iconPath: '/static/pages/images/sort.png',
  1818. selectedIconPath: '/static/pages/images/' + publics.iconSrc + 'sort_h.png',
  1819. iconPathOn: "sort.png",
  1820. selectedIconPathOn: 'sort_h.png',
  1821. pagePath: 'pages/service/index',
  1822. click: 'goService',
  1823. channel: 'service',
  1824. index: 2,
  1825. show: true
  1826. },
  1827. {
  1828. text: "购物车",
  1829. iconPath: '/static/pages/images/cart.png',
  1830. selectedIconPath: '/static/pages/images/' + publics.iconSrc + 'cart_h.png',
  1831. iconPathOn: "cart.png",
  1832. selectedIconPathOn: 'cart_h.png',
  1833. pagePath: 'pages/cart/cart?isSwitchTab=true',
  1834. click: 'goCart',
  1835. channel: 'cart',
  1836. index: 3,
  1837. show: true
  1838. },
  1839. {
  1840. text: "我的",
  1841. iconPath: '/static/pages/images/user.png',
  1842. selectedIconPath: '/static/pages/images/' + publics.iconSrc + 'user_h.png',
  1843. iconPathOn: "user.png",
  1844. selectedIconPathOn: 'user_h.png',
  1845. pagePath: 'pages/user/user',
  1846. click: 'goUser',
  1847. channel: 'user',
  1848. index: 4,
  1849. show: true
  1850. }
  1851. ]
  1852. }
  1853. ]
  1854. module.exports = {
  1855. setStorage: setStorage,
  1856. getStorage: getStorage,
  1857. postRequest: postRequest,
  1858. postRequestLoding: postRequestLoding,
  1859. getRequest: getRequest,
  1860. putRequest: putRequest,
  1861. putRequestJson: putRequestJson,
  1862. getRequestKDX: getRequestKDX,
  1863. postRequestKDX: postRequestKDX,
  1864. msg: msg,
  1865. g: g,
  1866. p: p,
  1867. env: env,
  1868. send: send,
  1869. redirectIndex: redirectIndex,
  1870. saveImage: saveImage,
  1871. saveImageToPhotosAlbum: saveImageToPhotosAlbum,
  1872. uploadFile: uploadFile,
  1873. msgConfirm: msgConfirm,
  1874. getConfig: getConfig,
  1875. load: load,
  1876. isLogin: isLogin,
  1877. isLogins: isLogins,
  1878. isAuthFn:isAuthFn,
  1879. payOrder: payOrder,
  1880. payOrders: payOrders,
  1881. receipt: receipt,
  1882. getLocation: getLocation,
  1883. scopeAddress: scopeAddress,
  1884. isAuth: isAuth,
  1885. alertMsg: alertMsg,
  1886. getToken: getToken,
  1887. loadIng: loadIng,
  1888. removeStorage: removeStorage,
  1889. public: publics,
  1890. header: header,
  1891. // gettmplIds: gettmplIds,
  1892. excludeSpecial: excludeSpecial,
  1893. tab: tab,
  1894. msgConfirmText: msgConfirmText,
  1895. silenceLogin: silenceLogin,
  1896. getWXUserProfile: getWXUserProfile,
  1897. updataWxInfos: updataWxInfos,
  1898. saveBehavior: saveBehavior,
  1899. getBehavior: getBehavior,
  1900. getBehaviorCount: getBehaviorCount,
  1901. getBehaviorUpdate: getBehaviorUpdate,
  1902. isTabPage: isTabPage,
  1903. toTabBar: toTabBar,
  1904. updataWxInfos: updataWxInfos,
  1905. initIMChat: initIMChat,
  1906. getIMToken: getIMToken,
  1907. jumpConversation: jumpConversation,
  1908. jumpSingleChatRoom: jumpSingleChatRoom,
  1909. onNetworkStatusChangeIM: onNetworkStatusChangeIM,
  1910. payAppOrder: payAppOrder,
  1911. savePortAndSource: savePortAndSource,
  1912. getUserPort: getUserPort,
  1913. getWebchatAppId: getWebchatAppId,
  1914. checkPaySuccess: paySuccess,
  1915. getwxConfig:getwxConfig
  1916. }