request.js 48 KB

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