index.js 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244
  1. const that = getApp()
  2. var sessionTimer;
  3. var recordTimeInterval = 0;
  4. var formidSubmitTimer;
  5. const recorderManager = wx.getRecorderManager() //创建录音
  6. const innerAudioContext = wx.createInnerAudioContext() //播放录音
  7. const util = require('../zoosutils/util.js')
  8. // 引入SDK核心类
  9. var QQMapWX = require('../zoosutils/qqmap-wx-jssdk.js');
  10. // 实例化API核心类
  11. var qqmapsdk = new QQMapWX({
  12. key: 'LGOBZ-LUVE6-2P3ST-MYLJK-UV2SZ-MXFUN' // 必填
  13. });
  14. //轨迹
  15. var ZOOSTRSDK = require('../zoosutils/common.js');
  16. Page({
  17. // 以下为用户填写参数
  18. appid: "wxb4ce09b25dab3bf9", //填写appid
  19. secretkey: "e9980f351a30693d2b4ee99b6dc6580f",
  20. siteid: "PLT47075192", //填写siteid
  21. // 以上为用户填写参数
  22. msgtick: 0,
  23. bbb: "1",
  24. ifgetfous: "0", //是否获取过焦点 0没获取过 1获取过
  25. globalUserData: {
  26. serviceurl: "https://wkbservice.yiaitao.cn/", //"https://wkbservice.yiaitao.cn/",//,
  27. openid: "",
  28. username: "",
  29. userheadurl: "",
  30. avatarUrl: "",
  31. gender: "",
  32. province: "",
  33. city: "",
  34. country: "",
  35. language: "",
  36. userPhone: "",
  37. },
  38. //设备信息
  39. system: "",
  40. model: "",
  41. //微信版本号
  42. wechatVersionNumber: "",
  43. isAtTalkPage: true, //当前是否在对话页面
  44. scene: "",
  45. companyinfo: {},
  46. serverconncet: false, //正常开启对话是否成功
  47. sence: "",
  48. //企业信息缓存(小程序打开,访问服务器可返回)
  49. bisLoginAuth: false, //用户信息是否已经获取完成
  50. getsubscribemsgcontentList: [], //获取订阅模版消息(客服发送的关键词)["111", "222", "333"]
  51. nowsendmsgcount: 1, // 当前发送消息数量
  52. data: {
  53. receivecus: "", //准备接待当前访客客服名称(对接SDK传入)
  54. showModalStatus: false,
  55. consumerheadurl: "../zoosimg/user.png",
  56. scrollTop: 0,
  57. msgcontent: "", //放消息内容
  58. msg: [],
  59. nowtalksid: "", //一次对话标识
  60. btnSendOrAddMenu: 1, //菜单或者发送按钮 0:发送 1:菜单
  61. menuheight: "110rpx",
  62. menrtr: "220rpx",
  63. menrtn: '110rpx',
  64. funhight: "55rpx", //用于计算底侧功能功能高度
  65. textareahight: "48rpx", //用于计算底侧textarea栏高度
  66. menushow: "none",
  67. isshowemoji: "none", //表情默认隐藏
  68. talkviewheight: "", //对话页面高度
  69. isvoice: 1, //1: 文字 2:语音
  70. keyboardOrvoice: "voice", //显示语音按钮或者键盘
  71. voiceBtnStyle: false, //语音按钮样式
  72. currentTab: 0, //预设当前项的值
  73. scrollLeft: 0, //tab标题的滚动条位置
  74. textareacursor: 0, //失去焦点时候获取光标位置
  75. txtfocus: false, //文本输入框获取焦点
  76. isSendVoice: true, //是否发送语音
  77. scrollViewHeight: "", //聊天窗口高度
  78. swiperheight: "350rpx",
  79. indicatorDots: true,
  80. vertical: false,
  81. autoplay: true,
  82. circular: true,
  83. interval: 2000,
  84. duration: 500,
  85. previousMargin: 0,
  86. nextMargin: 0,
  87. //解决部分机型没有发送按钮(小米,红米)暂时先判断是否是安卓还是ios
  88. bisexist: false, //是否存在 mark2
  89. timedifference: 0,
  90. ifreachessixty: false, //改变量判断是否时间超过60s 自动发送(用于阻止第二次调用停止语音方法,也用于自动发送后禁止上划取消)
  91. islastemjandtext: 0, //最后输入表情还是文字 0 文字 1表情
  92. // wxemojliList: [{
  93. // wxemojiid: "[10000+A]",
  94. // wxemojicontent: "😊"
  95. // }, {
  96. // wxemojiid: "[10001+A]",
  97. // wxemojicontent: "😍"
  98. // }, {
  99. // wxemojiid: "[10002+A]",
  100. // wxemojicontent: "😉"
  101. // }, {
  102. // wxemojiid: "[10003+A]",
  103. // wxemojicontent: "😃"
  104. // }, {
  105. // wxemojiid: "[10004+A]",
  106. // wxemojicontent: "😁"
  107. // }, {
  108. // wxemojiid: "[10005+A]",
  109. // wxemojicontent: "😝"
  110. // }, {
  111. // wxemojiid: "[10006+A]",
  112. // wxemojicontent: "😘"
  113. // }, {
  114. // wxemojiid: "[10007+A]",
  115. // wxemojicontent: "😚"
  116. // }, {
  117. // wxemojiid: "[10008+A]",
  118. // wxemojicontent: "😷"
  119. // }, {
  120. // wxemojiid: "[10009+A]",
  121. // wxemojicontent: "😏"
  122. // }, {
  123. // wxemojiid: "[10010+A]",
  124. // wxemojicontent: "😣"
  125. // }, {
  126. // wxemojiid: "[10011+A]",
  127. // wxemojicontent: "😥"
  128. // }, {
  129. // wxemojiid: "[10012+A]",
  130. // wxemojicontent: "😜"
  131. // }, {
  132. // wxemojiid: "[10013+A]",
  133. // wxemojicontent: "😲"
  134. // }, {
  135. // wxemojiid: "[10014+A]",
  136. // wxemojicontent: "👽"
  137. // }, {
  138. // wxemojiid: "[10015+A]",
  139. // wxemojicontent: "😭"
  140. // }, {
  141. // wxemojiid: "[10016+A]",
  142. // wxemojicontent: "😡"
  143. // }, {
  144. // wxemojiid: "[10017+A]",
  145. // wxemojicontent: "😌"
  146. // }, ],
  147. wxemojliList: [{
  148. wxemojiid: "【微笑】",
  149. wxemojicontent: "1.gif",
  150. wxemojimarhing: "_&1.gif-/a_&" //emoji标识
  151. }, {
  152. wxemojiid: "【撇嘴】",
  153. wxemojicontent: "2.gif",
  154. wxemojimarhing: "_&2.gif-/a_&"
  155. }, {
  156. wxemojiid: "【色】",
  157. wxemojicontent: "3.gif",
  158. wxemojimarhing: "_&3.gif-/a_&"
  159. }, {
  160. wxemojiid: "【发呆】",
  161. wxemojicontent: "6.gif",
  162. wxemojimarhing: "_&6.gif-/a_&"
  163. }, {
  164. wxemojiid: "【得意】",
  165. wxemojicontent: "5.gif",
  166. wxemojimarhing: "_&5.gif-/a_&"
  167. }, {
  168. wxemojiid: "【流泪】",
  169. wxemojicontent: "10.gif",
  170. wxemojimarhing: "_&10.gif-/a_&"
  171. }, {
  172. wxemojiid: "【害羞】",
  173. wxemojicontent: "7.gif",
  174. wxemojimarhing: "_&7.gif-/a_&"
  175. }, {
  176. wxemojiid: "【闭嘴】",
  177. wxemojicontent: "8.gif",
  178. wxemojimarhing: "_&8.gif-/a_&"
  179. },
  180. {
  181. wxemojiid: "【睡】",
  182. wxemojicontent: "9.gif",
  183. wxemojimarhing: "_&9.gif-/a_&"
  184. },
  185. {
  186. wxemojiid: "【尴尬】",
  187. wxemojicontent: "11.gif",
  188. wxemojimarhing: "_&11.gif-/a_&"
  189. },
  190. {
  191. wxemojiid: "【生气】",
  192. wxemojicontent: "12.gif",
  193. wxemojimarhing: "_&12.gif-/a_&"
  194. },
  195. {
  196. wxemojiid: "【调皮】",
  197. wxemojicontent: "13.gif",
  198. wxemojimarhing: "_&13.gif-/a_&"
  199. },
  200. {
  201. wxemojiid: "【呲牙】",
  202. wxemojicontent: "14.gif",
  203. wxemojimarhing: "_&14.gif-/a_&"
  204. },
  205. {
  206. wxemojiid: "【惊讶】",
  207. wxemojicontent: "15.gif",
  208. wxemojimarhing: "_&15.gif-/a_&"
  209. },
  210. {
  211. wxemojiid: "【难过】",
  212. wxemojicontent: "16.gif",
  213. wxemojimarhing: "_&16.gif-/a_&"
  214. }, {
  215. wxemojiid: "【囧】",
  216. wxemojicontent: "18.gif",
  217. wxemojimarhing: "_&18.gif-/a_&"
  218. }, {
  219. wxemojiid: "【抓狂】",
  220. wxemojicontent: "19.gif",
  221. wxemojimarhing: "_&19.gif-/a_&"
  222. }, {
  223. wxemojiid: "【吐】",
  224. wxemojicontent: "20.gif",
  225. wxemojimarhing: "_&20.gif-/a_&"
  226. }, {
  227. wxemojiid: "【偷笑】",
  228. wxemojicontent: "21.gif",
  229. wxemojimarhing: "_&21.gif-/a_&"
  230. }, {
  231. wxemojiid: "【愉快】",
  232. wxemojicontent: "22.gif",
  233. wxemojimarhing: "_&22.gif-/a_&"
  234. }, {
  235. wxemojiid: "【白眼】",
  236. wxemojicontent: "23.gif",
  237. wxemojimarhing: "_&23.gif-/a_&"
  238. }, {
  239. wxemojiid: "【傲慢】",
  240. wxemojicontent: "24.gif",
  241. wxemojimarhing: "_&24.gif-/a_&"
  242. }, {
  243. wxemojiid: "【困】",
  244. wxemojicontent: "26.gif",
  245. wxemojimarhing: "_&26.gif-/a_&"
  246. }, {
  247. wxemojiid: "【惊恐】",
  248. wxemojicontent: "27.gif",
  249. wxemojimarhing: "_&27.gif-/a_&"
  250. }, {
  251. wxemojiid: "【流汗】",
  252. wxemojicontent: "28.gif",
  253. wxemojimarhing: "_&28.gif-/a_&"
  254. }, {
  255. wxemojiid: "【憨笑】",
  256. wxemojicontent: "29.gif",
  257. wxemojimarhing: "_&29.gif-/a_&"
  258. }, {
  259. wxemojiid: "【悠闲】",
  260. wxemojicontent: "30.gif",
  261. wxemojimarhing: "_&30.gif-/a_&"
  262. }, {
  263. wxemojiid: "【奋斗】",
  264. wxemojicontent: "31.gif",
  265. wxemojimarhing: "_&31.gif-/a_&"
  266. }, {
  267. wxemojiid: "【咒骂】",
  268. wxemojicontent: "32.gif",
  269. wxemojimarhing: "_&32.gif-/a_&"
  270. }, {
  271. wxemojiid: "【疑问】",
  272. wxemojicontent: "33.gif",
  273. wxemojimarhing: "_&33.gif-/a_&"
  274. }, {
  275. wxemojiid: "【嘘】",
  276. wxemojicontent: "34.gif",
  277. wxemojimarhing: "_&34.gif-/a_&"
  278. }, {
  279. wxemojiid: "【晕】",
  280. wxemojicontent: "35.gif",
  281. wxemojimarhing: "_&35.gif-/a_&"
  282. }, {
  283. wxemojiid: "【衰】",
  284. wxemojicontent: "37.gif",
  285. wxemojimarhing: "_&37.gif-/a_&"
  286. }, {
  287. wxemojiid: "【骷髅】",
  288. wxemojicontent: "38.gif",
  289. wxemojimarhing: "_&38.gif-/a_&"
  290. }, {
  291. wxemojiid: "【敲打】",
  292. wxemojicontent: "39.gif",
  293. wxemojimarhing: "_&39.gif-/a_&"
  294. }, {
  295. wxemojiid: "【再见】",
  296. wxemojicontent: "40.gif",
  297. wxemojimarhing: "_&40.gif-/a_&"
  298. }, {
  299. wxemojiid: "【擦汗】",
  300. wxemojicontent: "41.gif",
  301. wxemojimarhing: "_&41.gif-/a_&"
  302. }, {
  303. wxemojiid: "【抠鼻】",
  304. wxemojicontent: "42.gif",
  305. wxemojimarhing: "_&42.gif-/a_&"
  306. }, {
  307. wxemojiid: "【鼓掌】",
  308. wxemojicontent: "43.gif",
  309. wxemojimarhing: "_&43.gif-/a_&"
  310. }, {
  311. wxemojiid: "【坏笑】",
  312. wxemojicontent: "45.gif",
  313. wxemojimarhing: "_&45.gif-/a_&"
  314. }, {
  315. wxemojiid: "【左哼哼】",
  316. wxemojicontent: "46.gif",
  317. wxemojimarhing: "_&46.gif-/a_&"
  318. }, {
  319. wxemojiid: "【右哼哼】",
  320. wxemojicontent: "47.gif",
  321. wxemojimarhing: "_&47.gif-/a_&"
  322. }, {
  323. wxemojiid: "【哈欠】",
  324. wxemojicontent: "48.gif",
  325. wxemojimarhing: "_&48.gif-/a_&"
  326. }, {
  327. wxemojiid: "【鄙视】",
  328. wxemojicontent: "49.gif",
  329. wxemojimarhing: "_&49.gif-/a_&"
  330. }, {
  331. wxemojiid: "【委屈】",
  332. wxemojicontent: "50.gif",
  333. wxemojimarhing: "_&50.gif-/a_&"
  334. }, {
  335. wxemojiid: "【快哭了】",
  336. wxemojicontent: "51.gif",
  337. wxemojimarhing: "_&51.gif-/a_&"
  338. }, {
  339. wxemojiid: "【阴险】",
  340. wxemojicontent: "52.gif",
  341. wxemojimarhing: "_&52.gif-/a_&"
  342. }, {
  343. wxemojiid: "【亲亲】",
  344. wxemojicontent: "53.gif",
  345. wxemojimarhing: "_&53.gif-/a_&"
  346. }
  347. ],
  348. //自定义表情集合(用来判断msgcontent是否存在表情)
  349. companyMenu: "none",
  350. isShowPhoneAuth: "none",
  351. //复制相关
  352. copyContentMsgid: "", //复制内容msgid
  353. copyCotent: "", //复制内容
  354. copyButtonPosition: 0,
  355. ifShowGetPhoneAuthBtn: false, //请联系我button是否获取电话信息
  356. //下拉相关
  357. ifscrollToupper: true, //是否触发下拉事件
  358. ifloading: true, //是否显示下拉数据
  359. loadingcontent: "",
  360. ifshowhistorymsg: "none",
  361. recordBtnstate: "0", //录音按钮状态 0按 1不按
  362. selPlayVoiceMsgid: "", //当前播放语音的msgid
  363. soundrecord_state: 0, //是否显示录音弹窗 0 不显示 1显示
  364. soundrecord_tiptext: "", //弹窗标题
  365. curphone: "", //临时贮存电话号码
  366. msgTextAndImg: "", //待拆分的消息(文字图片)
  367. scrollheigth: "",
  368. anewOpenChatid: 0, //重新开启对话标识 0 未重新开启 1重新开启
  369. anewOpenChatContent: "", //记录发送失败内容(重新开启对话)
  370. anewOpenChatType: "", //记录发送失败类型(重新开启对话)
  371. anewOpenChatMsgId: "",
  372. anewOpenChatNum: 0, //重新开启对话次数
  373. },
  374. onLoad(query) {
  375. var that = this;
  376. wx.setNavigationBarTitle({
  377. title: '在线客服'
  378. });
  379. this.setData({
  380. receivecus: query.receivecus,
  381. })
  382. wx.getSystemInfo({
  383. success: function(res) {
  384. that.system = res.platform;
  385. that.model = res.model;
  386. that.wechatVersionNumber = res.version;
  387. console.log(that.model);
  388. var padtop = 0;
  389. var padright = 0;
  390. var txthight = 0;
  391. var btnsendshow = 0; //解决部分机型没有发送按钮(小米,红米)暂时先判断是否是安卓还是ios
  392. var copytxtMode = 0; //控制复制方式
  393. if (that.system == "android") {
  394. padtop = 13;
  395. padright = 20;
  396. txthight = 40;
  397. btnsendshow = 0;
  398. } else if (that.system == "ios") {
  399. padtop = 4;
  400. padright = 20;
  401. txthight = 62;
  402. btnsendshow = 1;
  403. } else {
  404. padtop = 13;
  405. padright = 20;
  406. txthight = 40;
  407. btnsendshow = 1;
  408. }
  409. // 1 版本号大于 0版本号等于 -1版本号小于 (6.7.0)
  410. if (that.compareVersion(that.wechatVersionNumber, "6.7.0") < 0) {
  411. copytxtMode = 1;
  412. }
  413. that.setData({
  414. padtop: padtop,
  415. padright: padright,
  416. txthight: txthight,
  417. btnsendshow: btnsendshow,
  418. copytxtMode: copytxtMode
  419. })
  420. },
  421. });
  422. //获取secretkey
  423. if (that.secretkey.length == 0) {
  424. wx.request({
  425. //url: "http://localhost:62825/api/UserDockingService.ashx?action=getsecretkey",
  426. url: that.globalUserData.serviceurl +
  427. "/api/UserDockingService.ashx?action=getsecretkey",
  428. header: {
  429. 'content-type': 'application/json' // 默认值
  430. },
  431. data: {
  432. siteid: that.siteid,
  433. },
  434. method: "POST",
  435. success: function(e) {
  436. console.log("secretkeysecretkey" + e);
  437. //成功回调
  438. if (e.data.success) {
  439. that.secretkey = e.data.secretkey;
  440. that.login();
  441. } else {
  442. that.errorMsg("缺少密钥!");
  443. return;
  444. }
  445. },
  446. error: function(res) {
  447. that.errorMsg("获取密钥错误!");
  448. return;
  449. },
  450. })
  451. } else {
  452. that.login();
  453. }
  454. },
  455. login: function() {
  456. var scrollheigth = "";
  457. var that = this;
  458. console.log("vvvvv" + that.secretkey)
  459. wx.login({
  460. success: res => {
  461. console.log("11");
  462. // 发送 res.code 到后台换取 openId, sessionKey, unionId
  463. if (res.code) {
  464. console.log("11:" + res.code);
  465. var code = res.code;
  466. wx.request({
  467. //url: "http://localhost:62825/api/UserDockingService.ashx?action=getopenid",
  468. url: that.globalUserData.serviceurl +
  469. "/api/UserDockingService.ashx?action=getopenid",
  470. header: {
  471. 'content-type': 'application/json' // 默认值
  472. },
  473. data: {
  474. code: code, //
  475. scene: "",
  476. appid: that.appid,
  477. appsecret: that.secretkey
  478. },
  479. method: "POST",
  480. success: function(e) {
  481. console.log("444");
  482. //成功回调
  483. if (e.data.success) {
  484. console.log("22");
  485. that.globalUserData.openid = e.data.data;
  486. that.globalUserData.session_key = e.data
  487. .session_key;
  488. that.loadinghide();
  489. } else {
  490. //保证对话 请求失败,即随机生成
  491. that.globalUserData.openid = that.createCode(10);
  492. }
  493. },
  494. error: function(res) {
  495. //保证对话 请求失败,即随机生成
  496. that.globalUserData.openid = that.createCode(10);
  497. },
  498. complete: function() {
  499. that.openChat();
  500. }
  501. })
  502. }
  503. }
  504. })
  505. //控件
  506. var item = wx.createSelectorQuery();
  507. item.select('#boxview').boundingClientRect() //里面需要绑定 view组件的id
  508. item.exec(function(res) { //res为绑定元素信息的数组
  509. that.data.scrollheigth = res[0].top;
  510. if (that.system == "android") {
  511. scrollheigth = (res[0].top); //这样可以动态获取高度
  512. } else if (that.system == "ios") {
  513. scrollheigth = (res[0].top); //这样可以动态获取高度
  514. } else {
  515. scrollheigth = (res[0].top); //这样可以动态获取高度
  516. }
  517. console.log("--------------------" + scrollheigth);
  518. that.setData({
  519. scrollViewHeight: scrollheigth - 9
  520. })
  521. // that.loadingshow();
  522. // setTimeout(function () {
  523. // that.onCompanyMessage();
  524. // }, 500)
  525. })
  526. },
  527. onShow() {
  528. var that = this;
  529. //录音开始回调方法
  530. recorderManager.onStart(function(res) {
  531. if (that.data.recordBtnstate == "0") {
  532. recorderManager.stop();
  533. }
  534. })
  535. //录音结束回调用该方法
  536. recorderManager.onStop(function(res) {
  537. // wx.hideToast();
  538. clearInterval(recordTimeInterval);
  539. that.setData({
  540. soundrecord_state: 0,
  541. soundrecord_tiptext: ""
  542. })
  543. var msg = that.data.msg;
  544. var timedifference = that.data.timedifference;
  545. var voiceSrc = res.tempFilePath;
  546. var isSendVoice = that.data.isSendVoice
  547. console.log(res);
  548. if (timedifference < 1) {
  549. wx.showToast({
  550. title: '说话时间太短!',
  551. image: "../zoosimg/errorshow.png",
  552. duration: 800
  553. })
  554. } else if (!that.data.isSendVoice) {
  555. console.log("isSendVoice为:" + that.data.isSendVoice);
  556. } else {
  557. that.uploadfileToServer("2", voiceSrc, ".mp3");
  558. }
  559. // that.setData({
  560. // // timedifference: newtimeDifference,
  561. // voiceBtnStyle: false,
  562. // timedifference: timedifference,
  563. // })
  564. });
  565. //录音异常回调用该方法
  566. recorderManager.onError(function(res) {
  567. that.setData({
  568. soundrecord_state: 0,
  569. })
  570. if (res.errMsg == "operateRecorder:fail auth deny" || res.errMsg ==
  571. "operateRecorder:fail authorize no response" || res.errMsg ==
  572. "operateRecorder:fail:auth denied") {
  573. // wx.authorize({
  574. // scope: 'scope.record',
  575. // fail() {
  576. // console.log("第一次录音授权失败");
  577. wx.showModal({
  578. title: '提示',
  579. content: '您未授权录音,功能将无法使用',
  580. showCancel: true,
  581. confirmText: "授权",
  582. confirmColor: "#52a2d8",
  583. success: function(res) {
  584. if (res.confirm) {
  585. //确认则打开设置页面(重点)
  586. wx.openSetting({
  587. success: (res) => {
  588. console.log(res.authSetting);
  589. if (!res.authSetting['scope.record']) {
  590. //未设置录音授权
  591. console.log("未设置录音授权");
  592. wx.showModal({
  593. title: '提示',
  594. content: '您未授权录音,功能将无法使用',
  595. showCancel: false,
  596. success: function(res) {},
  597. })
  598. } else {
  599. //第二次才成功授权
  600. console.log("设置录音授权成功");
  601. wx.showModal({
  602. title: '提示',
  603. content: '授权成功!',
  604. showCancel: false,
  605. success: function(res) {},
  606. })
  607. // that.setData({
  608. // status: 2,
  609. // })
  610. }
  611. },
  612. fail: function() {
  613. console.log("授权设置录音失败");
  614. }
  615. })
  616. } else if (res.cancel) {
  617. console.log("cancel");
  618. }
  619. },
  620. fail: function() {
  621. console.log("openfail");
  622. }
  623. })
  624. // }
  625. // })
  626. }
  627. });
  628. },
  629. //页面隐藏操作
  630. onHide: function() {
  631. var that = this;
  632. wx.hideToast();
  633. clearInterval(recordTimeInterval);
  634. that.setData({
  635. voiceBtnStyle: false,
  636. isSendVoice: false,
  637. })
  638. },
  639. powerDrawer: function(e) {
  640. // wx.openUrl({ url: "https://wkbservice.yiaitao.cn/PublicityPage/LGN50496114/index.html", fail: res => { console.log(res) } });
  641. //button 获取formid
  642. if (e.detail.formId == 'the formId is a mock one' || e.detail.formId == undefined) {} else {
  643. // var item = {
  644. // formid: e.detail.formId,
  645. // formidcreatetime: util.formatTime(new Date())
  646. // }
  647. // this.data.formidArray.push(item);
  648. this.data.formid = e.detail.formId;
  649. this.data.formidcreatetime = util.formatTime(new Date());
  650. this.formidUpload();
  651. }
  652. var currentStatu = e.currentTarget.dataset.statu;
  653. this.util(currentStatu)
  654. },
  655. util: function(currentStatu) {
  656. /* 动画部分 */
  657. // 第1步:创建动画实例
  658. var animation = wx.createAnimation({
  659. duration: 200, //动画时长
  660. timingFunction: "linear", //线性
  661. delay: 0 //0则不延迟
  662. });
  663. // 第2步:这个动画实例赋给当前的动画实例
  664. this.animation = animation;
  665. // 第3步:执行第一组动画
  666. animation.opacity(0).rotateX(-100).step();
  667. // 第4步:导出动画对象赋给数据对象储存
  668. this.setData({
  669. animationData: animation.export()
  670. })
  671. // 第5步:设置定时器到指定时候后,执行第二组动画
  672. setTimeout(function() {
  673. // 执行第二组动画
  674. animation.opacity(1).rotateX(0).step();
  675. // 给数据对象储存的第一组动画,更替为执行完第二组动画的动画对象
  676. this.setData({
  677. animationData: animation
  678. })
  679. //关闭
  680. if (currentStatu == "close") {
  681. this.setData({
  682. showModalStatus: false
  683. });
  684. }
  685. }.bind(this), 200)
  686. // 显示
  687. if (currentStatu == "open") {
  688. this.setData({
  689. showModalStatus: true
  690. });
  691. }
  692. },
  693. //下拉加载
  694. onPullDownRefresh: function() {
  695. wx.showNavigationBarLoading() //在标题栏中显示加载
  696. //模拟加载
  697. setTimeout(function() {
  698. // complete
  699. wx.hideNavigationBarLoading() //完成停止加载
  700. wx.stopPullDownRefresh() //停止下拉刷新
  701. }, 1500);
  702. },
  703. //开启一个对话
  704. openChat: function() {
  705. var that = this;
  706. var newcode = "LRWT_" + that.createCode(19);
  707. console.log("that.globalUserData.openid;:::" + that.globalUserData.openid);
  708. // that.setData({
  709. // nowtalksid: newcode
  710. // })
  711. var data = {
  712. openid: that.globalUserData.openid + "1",
  713. username: wx.getStorageSync('userInfo').username || '',
  714. avatarurl: wx.getStorageSync('userInfo').avatar || '',
  715. gender: wx.getStorageSync('userInfo').gender || '',
  716. province: that.globalUserData.province,
  717. city: that.globalUserData.city,
  718. country: that.globalUserData.country,
  719. language: that.globalUserData.language,
  720. cid: "LRWT_" + that.globalUserData.openid,
  721. siteid: that.siteid,
  722. sid: newcode,
  723. visitcount: "1",
  724. ots: "0",
  725. source: that.sence == null ? "测试" : that.sence,
  726. mobile: wx.getStorageSync('userInfo').mobile || '',
  727. // receivecus: that.data.receivecus || '', //接待客服
  728. receivecus: '客服01', //接待客服
  729. trail: JSON.stringify(ZOOSTRSDK.CONFIG.trailJarr), //轨迹
  730. }
  731. console.log("客服连接中", data);
  732. wx.request({
  733. //url: "http://localhost:62825/api/Chat.ashx?action=open",
  734. //url: "https://wkbservice.yiaitao.cn/api/Chat.ashx?action=open",
  735. url: that.globalUserData.serviceurl + "/api/UserDockingService.ashx?action=open",
  736. header: {
  737. 'content-type': 'thatlication/json' // 默认值
  738. },
  739. data: data,
  740. method: "POST",
  741. success: function(e) {
  742. //成功回调
  743. var threeArray = [];
  744. if (e.data.success) {
  745. console.log("链接客服成功!");
  746. that.serverconncet = true;
  747. ZOOSTRSDK.CONFIG.trailJarr = [];
  748. // var cusheadurl = '';
  749. // if (that.globalUserData.avatarUrl.length > 0) {
  750. // cusheadurl = that.globalUserData.avatarUrl;
  751. // }
  752. var resultmsg = e.data.msgs;
  753. for (var j = 0; j < resultmsg.length; j++) {
  754. for (var i = 0; i < that.data.msg.length; i++) {
  755. if (that.data.msg[i].msgid == resultmsg[j].msgid) {
  756. that.data.msg.splice(i, 1);
  757. }
  758. }
  759. }
  760. for (var k = 0; k < resultmsg.length; k++) {
  761. if (resultmsg[k].content != null) {
  762. resultmsg[k].content = that.replacecontent(resultmsg[k].content,
  763. /\\/g, "/");
  764. var nowdate = util.formatTime(new Date()).substring(0, 10);
  765. if (resultmsg[k].msgtype == 2) {
  766. var msgparsecontent = JSON.parse(resultmsg[k].content);
  767. var msgcontent = msgparsecontent.mcontent;
  768. var timedifference = msgparsecontent.timedifference;
  769. var voicewidth = Number(timedifference); //计算语音宽度
  770. if (voicewidth >= 60) {
  771. voicewidth = 60;
  772. }
  773. var item = {
  774. content: msgcontent,
  775. msgid: resultmsg[k].msgid,
  776. msgtype: resultmsg[k].msgtype,
  777. source: resultmsg[k].source,
  778. tick: resultmsg[k].tick,
  779. time: resultmsg[k].time.substring(0, 10) == nowdate ?
  780. resultmsg[k].time.substring(11, resultmsg[k].time
  781. .length) : resultmsg[k].time,
  782. timedifference: timedifference,
  783. voicewidth: voicewidth
  784. }
  785. resultmsg[k] = item;
  786. } else if (resultmsg[k].msgtype == 0) {
  787. var item = {};
  788. var viewContent = that.splitMsgcontent(resultmsg[k].content);
  789. var item = {
  790. viewContent: viewContent,
  791. content: resultmsg[k].content,
  792. msgid: resultmsg[k].msgid,
  793. msgtype: resultmsg[k].msgtype,
  794. source: resultmsg[k].source,
  795. tick: resultmsg[k].tick,
  796. time: resultmsg[k].time.substring(0, 10) == nowdate ?
  797. resultmsg[k].time.substring(11, resultmsg[k].time
  798. .length) : resultmsg[k].time,
  799. timedifference: timedifference,
  800. voicewidth: voicewidth
  801. }
  802. resultmsg[k] = item;
  803. }
  804. that.data.msg.push(resultmsg[k]);
  805. }
  806. }
  807. that.setData({
  808. msgArray: that.data.msg.sort(that.compare("tick")),
  809. // consumerheadurl: cusheadurl,
  810. scrollTop: 2147483647 + "px",
  811. msgcontent: "",
  812. nowtalksid: e.data.sid,
  813. });
  814. that.loadinghide();
  815. that.sync();
  816. //重新开启对话后发送失败消息
  817. if (that.data.anewOpenChatid == 1) {
  818. if (that.data.anewOpenChatNum == 3) {
  819. that.data.anewOpenChatNum = that.data.anewOpenChatNum++;
  820. that.data.msgtype = that.data.anewOpenChatType;
  821. that.data.msgcontent = that.data.anewOpenChatContent;
  822. that.sendMsg(that.data.anewOpenChatType, 0);
  823. } else {
  824. that.errorMsg("重新开启对话失败!" + e.data.error);
  825. return;
  826. }
  827. }
  828. // that.formidSync();
  829. } else {
  830. if (e.data.error.indexOf(
  831. 'HTTP Error 404. The requested resource is not found.') == 1) {
  832. that.errorMsg('HTTP Error 404!');
  833. console.log(e.data.error);
  834. } else {
  835. that.errorMsg("开启对话失败!" + e.data.error);
  836. console.log(e.data.error);
  837. }
  838. }
  839. },
  840. error: function(e) {
  841. console.log("获取企业信息异常!");
  842. that.errorMsg("链接客服失败!");
  843. return
  844. },
  845. });
  846. },
  847. ok: function() {
  848. console.log("meun!");
  849. },
  850. //发送消息
  851. sendMsgBtn: function(e) {
  852. var that = this;
  853. that.setData({
  854. companyMenu: "none",
  855. })
  856. if (that.data.islastemjandtext == 0) {
  857. that.bbb = "0";
  858. }
  859. console.log("that.bbb:" + that.bbb);
  860. console.log("发消息");
  861. var noConnect = that.checkConnectOk();
  862. if (noConnect.length > 0) {
  863. that.errorMsg(noConnect);
  864. return;
  865. }
  866. if (that.data.msgcontent == "" || that.data.msgcontent == null || that.data.msgcontent ==
  867. undefined) {
  868. that.errorMsg("没有发送内容!");
  869. return;
  870. }
  871. // that.splitMsgcontent(); //拆分文字,图片
  872. that.sendMsg(0, 0);
  873. },
  874. //心跳
  875. sync: function() {
  876. var that = this;
  877. if (that.data.nowtalksid.length == 0) {
  878. return;
  879. }
  880. that.initInterval();
  881. sessionTimer = setInterval(function() {
  882. // 心跳
  883. wx.request({
  884. url: that.globalUserData.serviceurl +
  885. "/api/UserDockingService.ashx?action=sync",
  886. header: {
  887. 'content-type': 'thatlication/json' // 默认值
  888. },
  889. data: {
  890. siteid: that.siteid,
  891. sid: that.data.nowtalksid,
  892. tick: that.msgtick,
  893. },
  894. method: "POST",
  895. success: function(e) {
  896. //成功回调
  897. if (e.data.success) {
  898. if (e.data.list.length > 0) {
  899. var resultmsg = e.data.list;
  900. for (var j = 0; j < resultmsg.length; j++) {
  901. for (var i = 0; i < that.data.msg.length; i++) {
  902. if (that.data.msg[i].msgid == resultmsg[j].msgid) {
  903. that.data.msg.splice(i, 1);
  904. }
  905. }
  906. }
  907. for (var k = 0; k < resultmsg.length; k++) {
  908. if (resultmsg[k].content != null) {
  909. resultmsg[k].content = that.replacecontent(
  910. resultmsg[k].content, /\\/g, "/");
  911. var nowdate = util.formatTime(new Date()).substring(
  912. 0, 10);
  913. if (resultmsg[k].msgtype == 2) {
  914. var msgparsecontent = JSON.parse(resultmsg[k]
  915. .content);
  916. var msgcontent = msgparsecontent.mcontent;
  917. var timedifference = msgparsecontent
  918. .timedifference;
  919. var voicewidth = Number(
  920. timedifference); //计算语音宽度
  921. if (voicewidth >= 60) {
  922. voicewidth = 60;
  923. }
  924. var item = {};
  925. //客服消息
  926. item = {
  927. content: msgcontent,
  928. msgid: resultmsg[k].msgid,
  929. msgtype: resultmsg[k].msgtype,
  930. source: resultmsg[k].source,
  931. tick: resultmsg[k].tick,
  932. time: resultmsg[k].time.substring(0,
  933. 10) == nowdate ? resultmsg[k].time
  934. .substring(11, resultmsg[k].time
  935. .length) : resultmsg[k].time,
  936. timedifference: timedifference,
  937. voicewidth: voicewidth,
  938. getuser: "0",
  939. }
  940. resultmsg[k] = item;
  941. } else if (resultmsg[k].msgtype == 0) {
  942. var item = {};
  943. var viewContent = that.splitMsgcontent(
  944. resultmsg[k].content);
  945. item = {
  946. viewContent: viewContent,
  947. content: resultmsg[k].content,
  948. msgid: resultmsg[k].msgid,
  949. msgtype: resultmsg[k].msgtype,
  950. source: resultmsg[k].source,
  951. tick: resultmsg[k].tick,
  952. time: resultmsg[k].time.substring(0,
  953. 10) == nowdate ? resultmsg[k].time
  954. .substring(11, resultmsg[k].time
  955. .length) : resultmsg[k].time,
  956. timedifference: timedifference,
  957. getuser: "0",
  958. }
  959. resultmsg[k] = item;
  960. } else if (resultmsg[k].msgtype == 1) {
  961. var item = {};
  962. var viewContent = that.splitMsgcontent(
  963. resultmsg[k].content);
  964. item = {
  965. viewContent: viewContent,
  966. content: resultmsg[k].content,
  967. msgid: resultmsg[k].msgid,
  968. msgtype: resultmsg[k].msgtype,
  969. source: resultmsg[k].source,
  970. tick: resultmsg[k].tick,
  971. time: resultmsg[k].time.substring(0,
  972. 10) == nowdate ? resultmsg[k].time
  973. .substring(11, resultmsg[k].time
  974. .length) : resultmsg[k].time,
  975. timedifference: timedifference,
  976. getuser: "0",
  977. }
  978. resultmsg[k] = item;
  979. } else if (resultmsg[k].msgtype == 3) { //定位消息
  980. var msgparsecontent = JSON.parse(resultmsg[k]
  981. .content);
  982. var name = msgparsecontent.name;
  983. var address = msgparsecontent.address;
  984. var latitude = msgparsecontent.latitude;
  985. var longitude = msgparsecontent.longitude;
  986. var item = {};
  987. //客服消息
  988. item = {
  989. name: name,
  990. address: address,
  991. latitude: latitude,
  992. longitude: longitude,
  993. msgid: resultmsg[k].msgid,
  994. msgtype: resultmsg[k].msgtype,
  995. source: resultmsg[k].source,
  996. tick: resultmsg[k].tick,
  997. time: resultmsg[k].time.substring(0,
  998. 10) == nowdate ? resultmsg[k].time
  999. .substring(11, resultmsg[k].time
  1000. .length) : resultmsg[k].time,
  1001. getuser: "0",
  1002. }
  1003. resultmsg[k] = item;
  1004. }
  1005. that.data.msg.push(resultmsg[k]);
  1006. }
  1007. if (resultmsg[k].tick > that.msgtick) {
  1008. that.msgtick = resultmsg[k].tick;
  1009. }
  1010. }
  1011. that.setData({
  1012. msgArray: that.data.msg.sort(that.compare(
  1013. "tick")),
  1014. // scrollTop: 2147483647 + "px",
  1015. });
  1016. that.scrollToTheBottom();
  1017. // that.setData({
  1018. // scrollTop: 2147483647 + "px",
  1019. // })
  1020. }
  1021. } else {
  1022. if (e.data.error == "no chat" && that.isAtTalkPage) {
  1023. // that.openChat();
  1024. // console.log("已重新开启对话!");
  1025. } else {
  1026. console.log(e.data.error);
  1027. }
  1028. return false;
  1029. }
  1030. },
  1031. error: function(e) {
  1032. console.log("聊天消息获取异常!");
  1033. return
  1034. },
  1035. });
  1036. }, 1000)
  1037. },
  1038. //上传图片至服务器
  1039. uploadfileToServer: function(msgtype, fileurl, fileex) {
  1040. var that = this;
  1041. wx.uploadFile({
  1042. url: that.globalUserData.serviceurl + "/api/File.ashx",
  1043. filePath: fileurl,
  1044. name: 'file',
  1045. header: {
  1046. 'lr_siteid': that.siteid,
  1047. 'lr_fileex': fileex,
  1048. 'lr_filetype': '1', //格式 0:语音 1:图片
  1049. },
  1050. formData: [{
  1051. name: 'param1',
  1052. value: 'value1'
  1053. },
  1054. {
  1055. name: 'lr_siteid',
  1056. value: that.siteid
  1057. },
  1058. {
  1059. name: 'lr_fileex',
  1060. value: fileex
  1061. },
  1062. {
  1063. name: 'lr_filetype',
  1064. value: '1'
  1065. }
  1066. ],
  1067. // method: "POST",
  1068. success: function(e) {
  1069. //成功回调
  1070. var result = JSON.parse(e.data);
  1071. if (result.success) {
  1072. that.data.msgcontent = that.globalUserData.serviceurl + result.url,
  1073. that.sendMsg(msgtype, 0);
  1074. } else {
  1075. that.errorMsg("开启对话失败!" + result.error);
  1076. console.log(result.error);
  1077. }
  1078. },
  1079. error: function(e) {
  1080. console.log("获取企业信息异常!");
  1081. that.errorMsg("链接客服失败!");
  1082. return
  1083. },
  1084. });
  1085. },
  1086. //发送消息 msgtype:0,文字 1,图片 2,语音 3,定位
  1087. sendMsg: function(msgtype, msgsource) {
  1088. // that.errorMsg("4543534543");
  1089. var that = this;
  1090. var msgid = "";
  1091. if (that.data.anewOpenChatid != 1) {
  1092. msgid = that.createCode(32); //生成msgid 32位字符串
  1093. } else {
  1094. msgid = that.data.anewOpenChatMsgId;
  1095. }
  1096. //如果为0 需要禁止发送按钮 防止连击
  1097. if (msgtype == 0) {
  1098. that.setData({
  1099. disabled: "false"
  1100. })
  1101. setTimeout(function() {
  1102. that.setData({
  1103. disabled: "",
  1104. });
  1105. // that.data.formid = "";
  1106. // that.data.formidcreatetime = "";
  1107. }, 1000)
  1108. };
  1109. var mcontent;
  1110. var viewContent; //处理文字消息 拆分表情
  1111. //如果msgtype是2则为语音消息 需要传参数timedifference
  1112. if (msgtype == 2) {
  1113. var timeDifference = Number((Number(that.data.timedifference) / 2).toFixed(0));
  1114. var msgItem = {
  1115. mcontent: that.data.msgcontent,
  1116. timedifference: timeDifference
  1117. }
  1118. mcontent = JSON.stringify(msgItem);
  1119. //计算语音宽度
  1120. var voicewidth = timeDifference;
  1121. if (voicewidth >= 60) {
  1122. voicewidth = 60;
  1123. }
  1124. } else if (msgtype == 0) {
  1125. mcontent = that.data.msgcontent;
  1126. viewContent = that.splitMsgcontent(that.data.msgcontent);
  1127. } else if (msgtype == 3) {
  1128. var name = that.data.msgcontent.name;
  1129. var address = that.data.msgcontent.address;
  1130. var latitude = that.data.msgcontent.latitude;
  1131. var longitude = that.data.msgcontent.longitude;
  1132. mcontent = that.data.msgcontent;
  1133. console.log("ppppppp:" + name)
  1134. } else {
  1135. mcontent = that.data.msgcontent;
  1136. };
  1137. var nowtime = util.formatTime(new Date());
  1138. if (that.data.anewOpenChatid != 1) { //对话超时重启对话,该消息已经向缓存添加过
  1139. //现改为直接向页面赋值
  1140. var item = {
  1141. viewContent: viewContent,
  1142. content: mcontent,
  1143. msgid: msgid,
  1144. msgtype: msgtype,
  1145. source: msgsource,
  1146. tick: "999999999999999999", //生成最大tick 保证该消息在最后一条
  1147. time: nowtime.substring(11, nowtime.length),
  1148. timedifference: timeDifference,
  1149. voicewidth: voicewidth,
  1150. msgsendstate: "0", //信息发送状态 0 正在发送
  1151. };
  1152. that.data.msg.push(item);
  1153. that.setData({
  1154. msgArray: that.data.msg,
  1155. msgcontent: "",
  1156. // scrollTop: 2147483647 + "px",
  1157. textareacursor: 0,
  1158. btnSendOrAddMenu: 1,
  1159. funhight: "55rpx",
  1160. textareahight: "48rpx",
  1161. menushow: "none",
  1162. isshowemoji: "none",
  1163. menuheight: "110rpx",
  1164. });
  1165. that.scrollToTheBottom();
  1166. } else {
  1167. mcontent = that.data.anewOpenChatContent;
  1168. that.data.anewOpenChatid = 0;
  1169. }
  1170. wx.request({
  1171. //url: "https://wkbservice.yiaitao.cn/api/Chat.ashx?action=send",
  1172. // url: "http://localhost:62825/api/Chat.ashx?action=send",
  1173. url: that.globalUserData.serviceurl + "/api/UserDockingService.ashx?action=send",
  1174. header: {
  1175. 'content-type': 'thatlication/json' // 默认值
  1176. },
  1177. data: {
  1178. siteid: that.siteid,
  1179. sid: that.data.nowtalksid,
  1180. msg: mcontent,
  1181. source: msgsource,
  1182. msgtype: msgtype,
  1183. // formid: formid,
  1184. // formidcreatetime: formidcreatetime,
  1185. msgid: msgid,
  1186. },
  1187. method: "POST",
  1188. success: function(e) {
  1189. //成功回调
  1190. if (e.data.success) {} else {
  1191. if (e.data.error == "no chat" && that.isAtTalkPage) {
  1192. // that.setData({
  1193. // msgArray: that.arrarRemove(that.data.msg, msgid)
  1194. // });
  1195. that.data.anewOpenChatid = 1;
  1196. that.data.anewOpenChatType = msgtype;
  1197. that.data.anewOpenChatContent = mcontent;
  1198. that.data.anewOpenChatMsgId = msgid;
  1199. that.openChat();
  1200. console.log("已重新开启对话!");
  1201. } else {
  1202. console.log(e.data.error);
  1203. return false;
  1204. }
  1205. }
  1206. },
  1207. error: function(e) {
  1208. console.log("发送消息失败" + e.data.error);
  1209. return
  1210. },
  1211. complete: function() {
  1212. },
  1213. });
  1214. },
  1215. //语音按钮显示
  1216. voiceShowBtn: function(e) {
  1217. var that = this;
  1218. var isShowVioce = "1";
  1219. var boardOrvoice = "voice";
  1220. if (this.data.isvoice == "1") {
  1221. isShowVioce = "2";
  1222. boardOrvoice = "keyboard";
  1223. if (!that.data.isfirstUseRe) {
  1224. wx.authorize({
  1225. scope: 'scope.record',
  1226. success(res) {
  1227. // wx.startRecord()
  1228. },
  1229. error(res) {
  1230. console.log(res);
  1231. }
  1232. });
  1233. that.data.isfirstUseRe = true;
  1234. }
  1235. }
  1236. this.setData({
  1237. isvoice: isShowVioce,
  1238. menuheight: "110rpx",
  1239. menushow: "none",
  1240. isshowemoji: "none",
  1241. keyboardOrvoice: boardOrvoice,
  1242. funhight: "55rpx",
  1243. textareahight: "50rpx",
  1244. companyMenu: "none",
  1245. copyContentMsgid: "",
  1246. })
  1247. },
  1248. //长按开始: 录音开始
  1249. longtapStart: function(e) {
  1250. var that = this;
  1251. // wx.showToast({
  1252. // title: '正在录音',
  1253. // duration: 999999,
  1254. // image: '../zoosimg/luyin-ing.png'
  1255. // })
  1256. that.data.timedifference = 0;
  1257. that.data.isSendVoice = true;
  1258. this.setData({
  1259. // startseconds: startSeconds,
  1260. voiceBtnStyle: true,
  1261. startpointY: e.touches[0].pageY, //当前触摸点样y坐标抽
  1262. soundrecord_state: 1,
  1263. soundrecord_tiptext: "正在录音",
  1264. recordBtnstate: "1",
  1265. })
  1266. recorderManager.start({
  1267. format: "mp3",
  1268. // duration: 3000,
  1269. })
  1270. // 录音计时器
  1271. recordTimeInterval = setInterval(function() {
  1272. that.data.timedifference = that.data.timedifference + 1;
  1273. // console.log(that.data.timedifference);
  1274. if (that.data.timedifference == 120) {
  1275. that.data.ifreachessixty = true;
  1276. recorderManager.stop();
  1277. }
  1278. }, 500)
  1279. },
  1280. //触摸结束:录音结束
  1281. touchEnd: function(e) {
  1282. var that = this;
  1283. clearInterval(recordTimeInterval);
  1284. that.setData({
  1285. voiceBtnStyle: false,
  1286. // timedifference: that.data.timedifference,
  1287. recordBtnstate: "0",
  1288. })
  1289. if (!that.data.ifreachessixty) {
  1290. wx.hideToast();
  1291. recorderManager.stop();
  1292. } else {
  1293. that.data.ifreachessixty = false;
  1294. console.log("到达60s,以自动发出")
  1295. }
  1296. },
  1297. //手指触摸后移动:取消发送语音
  1298. cancelmove: function(e) {
  1299. var statrtY = this.data.startpointY;
  1300. var curpointY = e.touches[0].pageY;
  1301. if (statrtY - curpointY > 80) {
  1302. // recorderManager.stop();
  1303. wx.showToast({
  1304. title: '取消发送',
  1305. duration: 999999
  1306. })
  1307. this.setData({
  1308. isSendVoice: false,
  1309. })
  1310. }
  1311. },
  1312. // 播放录音
  1313. audioPlay: function(e) {
  1314. var that = this;
  1315. var src = e.currentTarget.dataset.src;
  1316. var msgid = e.currentTarget.dataset.id;
  1317. if (src == '') {
  1318. that.errorMsg("播放语音失败");
  1319. return;
  1320. }
  1321. //判断当前是否有语音正在播放
  1322. if (innerAudioContext.paused) {
  1323. innerAudioContext.src = src;
  1324. innerAudioContext.play();
  1325. // that.data.selPlayVoiceMsgid = msgid;
  1326. that.setData({
  1327. selPlayVoiceMsgid: msgid
  1328. })
  1329. } else {
  1330. innerAudioContext.stop();
  1331. if (msgid != that.data.selPlayVoiceMsgid) {
  1332. innerAudioContext.src = src;
  1333. innerAudioContext.play();
  1334. // that.data.selPlayVoiceMsgid = msgid;
  1335. that.setData({
  1336. selPlayVoiceMsgid: msgid
  1337. })
  1338. }
  1339. }
  1340. //监听音频自然播放至结束的事件
  1341. innerAudioContext.onEnded(function() {
  1342. that.setData({
  1343. selPlayVoiceMsgid: ""
  1344. })
  1345. })
  1346. //监听音频的异常事件
  1347. innerAudioContext.onError((res) => {
  1348. console.log(res.errMsg);
  1349. })
  1350. },
  1351. //添加表情
  1352. sendEmoji: function(e) {
  1353. console.log(e);
  1354. var that = this;
  1355. that.data.islastemjandtext = 1;
  1356. that.insertEmoji(that.data.msgcontent, e.target.dataset.id)
  1357. },
  1358. insertEmoji: function(originalString, flgString) {
  1359. var newstr = "";
  1360. var cursor = 0;
  1361. if (originalString.length != 0) {
  1362. cursor = this.data.textareacursor;
  1363. }
  1364. if (originalString.length > 0) {
  1365. if (cursor != 0) {
  1366. for (var i = 0; i < originalString.length; i++) {
  1367. if (i + 1 == cursor) {
  1368. var tmpstart = originalString.substring(0, cursor);
  1369. newstr += tmpstart + flgString;
  1370. cursor = cursor + flgString.length;
  1371. break;
  1372. }
  1373. }
  1374. } else {
  1375. newstr = flgString + originalString;
  1376. cursor = cursor + flgString.length;
  1377. }
  1378. } else {
  1379. newstr = flgString;
  1380. cursor = cursor + flgString.length;
  1381. }
  1382. this.setData({
  1383. msgcontent: newstr,
  1384. textareacursor: cursor,
  1385. btnSendOrAddMenu: 0,
  1386. })
  1387. },
  1388. //展示菜单
  1389. menuhide: function() {
  1390. this.setData({
  1391. menuheight: "110rpx",
  1392. menushow: "none"
  1393. })
  1394. },
  1395. // 相机 data-type = 2 or data-type = 1:相册
  1396. takeOrAlbumBtn: function(e) {
  1397. var that = this;
  1398. var typeoption = e.currentTarget.dataset.type == 1 ? ["album"] : ["camera"];
  1399. wx.chooseImage({
  1400. count: 1,
  1401. sizeType: ['original', 'compressed'],
  1402. sourceType: typeoption, //camera:相机 ,album:相册
  1403. success(res) {
  1404. // tempFilePath可以作为img标签的src属性显示图片
  1405. const tempFilePaths = res.tempFilePaths
  1406. that.uploadfileToServer("1", tempFilePaths[0], '.png');
  1407. },
  1408. error: function(e) {
  1409. console.log(e.msg);
  1410. that.errorMsg(e.msg);
  1411. },
  1412. fail: function(e) {
  1413. console.log(e.msg);
  1414. that.errorMsg(e.msg);
  1415. },
  1416. })
  1417. },
  1418. allmenuhide: function(e) {
  1419. console.log(e);
  1420. var that = this;
  1421. this.setData({
  1422. isshowemoji: "none",
  1423. menuheight: "110rpx",
  1424. menushow: "none",
  1425. companyMenu: "none",
  1426. companypop: "none",
  1427. contactUspop: "none",
  1428. funhight: "55rpx",
  1429. textareahight: "48rpx",
  1430. copyContentMsgid: "",
  1431. })
  1432. },
  1433. //点击banner
  1434. clickbanner: function(e) {
  1435. var that = this;
  1436. that.data.msgcontent = e.currentTarget.dataset.content;
  1437. that.sendMsg(0, 1);
  1438. },
  1439. // 点击客服头像
  1440. companyMenuShow: function(e) {
  1441. var that = this;
  1442. var left = e.detail.x;
  1443. var right = e.detail.y;
  1444. if (280 + right > this.data.scrollViewHeight.replace("px", "")) {
  1445. right = this.data.scrollViewHeight.replace("px", "") - 280;
  1446. }
  1447. this.setData({
  1448. companyMenu: "block",
  1449. companymenuleft: left + "px", //企业信息left
  1450. companymenuright: right + "px", //企业信息right
  1451. })
  1452. this.headclick = 1;
  1453. },
  1454. closeCompanypop: function() {
  1455. this.setData({
  1456. companypop: "none"
  1457. })
  1458. },
  1459. closeContactUspop: function() {
  1460. this.setData({
  1461. contactUspop: "none"
  1462. })
  1463. },
  1464. //展示表情
  1465. emojimenu: function() {
  1466. var menuhei;
  1467. var showemoji;
  1468. var txtgetfocus = false;
  1469. var funhight;
  1470. var textareahight;
  1471. if (this.data.isshowemoji == "none") {
  1472. menuhei = "570rpx"
  1473. showemoji = "block";
  1474. funhight = "510rpx"
  1475. textareahight = "500rpx"
  1476. } else {
  1477. menuhei = "110rpx"
  1478. showemoji = "none";
  1479. funhight = "55rpx"
  1480. textareahight = "48rpx"
  1481. }
  1482. this.setData({
  1483. menushow: "none",
  1484. menuheight: menuhei,
  1485. isshowemoji: showemoji,
  1486. // txtfocus: txtgetfocus,
  1487. isvoice: 1,
  1488. keyboardOrvoice: "voice",
  1489. funhight: funhight,
  1490. textareahight: textareahight,
  1491. companyMenu: "none",
  1492. copyContentMsgid: "",
  1493. })
  1494. },
  1495. //展示菜单
  1496. menuBtn: function() {
  1497. var menuhei;
  1498. var menu;
  1499. var txtgetfocus = false;
  1500. var funhight;
  1501. var textareahight;
  1502. if (this.data.menushow == 'none') {
  1503. menuhei = "570rpx"
  1504. menu = "flex";
  1505. funhight = "510rpx"
  1506. textareahight = "500rpx"
  1507. } else {
  1508. menuhei = "110rpx"
  1509. menu = "none";
  1510. funhight = "55rpx"
  1511. textareahight = "48rpx"
  1512. }
  1513. this.setData({
  1514. isshowemoji: "none",
  1515. menuheight: menuhei,
  1516. menushow: menu,
  1517. txtfocus: txtgetfocus,
  1518. isvoice: 1,
  1519. keyboardOrvoice: "voice",
  1520. funhight: funhight,
  1521. textareahight: textareahight,
  1522. companyMenu: "none",
  1523. copyContentMsgid: "",
  1524. })
  1525. },
  1526. talkviewbottomf: function() {
  1527. this.setData({
  1528. isshowemoji: "none",
  1529. menushow: "none",
  1530. companyMenu: "none",
  1531. copyContentMsgid: "",
  1532. })
  1533. this.ifgetfous = "1",
  1534. this.checkoutmsg();
  1535. },
  1536. //消息编辑框失去焦点
  1537. msgtxtbulr: function(e) {
  1538. var that = this;
  1539. var curcursor = e.detail.cursor; //失去焦点时候获取光标位置
  1540. if (that.data.isshowemoji == "none") {
  1541. that.setData({
  1542. textareacursor: curcursor,
  1543. isshowemoji: "none",
  1544. menuheight: "110rpx",
  1545. })
  1546. } else {
  1547. that.setData({
  1548. textareacursor: curcursor,
  1549. isshowemoji: "block",
  1550. })
  1551. }
  1552. if (that.data.menushow == "none") {
  1553. that.setData({
  1554. textareacursor: curcursor,
  1555. menushow: "none",
  1556. })
  1557. } else {
  1558. that.setData({
  1559. textareacursor: curcursor,
  1560. menushow: "flex",
  1561. menuheight: "570rpx",
  1562. })
  1563. };
  1564. if (that.data.menushow == "none" && that.data.isshowemoji == "none") {
  1565. that.setData({
  1566. funhight: "55rpx",
  1567. textareahight: "48rpx"
  1568. })
  1569. }
  1570. that.setData({
  1571. copyContentMsgid: "",
  1572. })
  1573. if (that.data.msgcontent.length > 0) {
  1574. that.ifgetfous = "0"
  1575. }
  1576. },
  1577. //输入法事件
  1578. checkoutmsg: function(e) {
  1579. var that = this;
  1580. that.data.islastemjandtext = 0;
  1581. if (that.system == "android") {
  1582. if (that.model == "Redmi Note 3" || that.model == "Mi Note 3" || that.model ==
  1583. "Redmi Note 4") {} else {
  1584. console.log("BBB:" + that.bbb);
  1585. if (that.bbb == "0" && that.ifgetfous == "1") {
  1586. that.bbb = "1";
  1587. if (e != undefined) {
  1588. that.ifgetfous = "0"
  1589. }
  1590. that.setData({
  1591. msgcontent: "",
  1592. })
  1593. return;
  1594. }
  1595. }
  1596. }
  1597. if (e != undefined) {
  1598. var sOrM = 1;
  1599. if (e.detail.value.replace(/\s+/g, '').length > 0) {
  1600. sOrM = 0;
  1601. }
  1602. that.setData({
  1603. btnSendOrAddMenu: sOrM,
  1604. msgcontent: e.detail.value,
  1605. })
  1606. }
  1607. },
  1608. // 滚动切换标签样式
  1609. switchTab: function(e) {
  1610. this.setData({
  1611. currentTab: e.detail.current
  1612. });
  1613. this.checkCor();
  1614. },
  1615. // 点击标题切换当前页时改变样式
  1616. swichNav: function(e) {
  1617. var cur = e.target.dataset.current;
  1618. if (this.data.currentTaB == cur) {
  1619. return false;
  1620. } else {
  1621. this.setData({
  1622. currentTab: cur
  1623. })
  1624. }
  1625. },
  1626. //判断当前滚动超过一屏时,设置tab标题滚动条。
  1627. checkCor: function() {
  1628. if (this.data.currentTab > 4) {
  1629. this.setData({
  1630. scrollLeft: 300
  1631. })
  1632. } else {
  1633. this.setData({
  1634. scrollLeft: 0
  1635. })
  1636. }
  1637. },
  1638. //排序
  1639. compare: function(property) {
  1640. return function(a, b) {
  1641. var value1 = a[property];
  1642. var value2 = b[property];
  1643. return value1 - value2;
  1644. }
  1645. },
  1646. //session 失效后,重新获取
  1647. getsessionksy: function() {
  1648. wx.login({
  1649. success: res => {
  1650. // 发送 res.code 到后台换取 openId, sessionKey, unionId
  1651. if (res.code) {
  1652. var code = res.code;
  1653. wx.request({
  1654. url: that.globalUserData.serviceurl +
  1655. "/api/UserDockingService.ashx?action=getopenid",
  1656. // url: "http://localhost:62825/api/thatletService.ashx?action=getopenid",
  1657. header: {
  1658. 'content-type': 'thatlication/json' // 默认值
  1659. },
  1660. data: {
  1661. code: code, //
  1662. scene: that.sence,
  1663. },
  1664. method: "POST",
  1665. success: function(e) {
  1666. //成功回调
  1667. if (e.data.success) {
  1668. that.globalUserData.session_key = e.data
  1669. .session_key;
  1670. that.setData({
  1671. ifShowGetPhoneAuthBtn: true
  1672. });
  1673. }
  1674. }
  1675. })
  1676. }
  1677. }
  1678. })
  1679. },
  1680. // 获取用户电话号码End
  1681. viewLargerImage: function(e) {
  1682. var src = e.currentTarget.dataset.src; //获取data-src
  1683. //图片预览
  1684. wx.previewImage({
  1685. current: src, // 当前显示图片的url
  1686. urls: [src], // 需要预览的图片utl链接列表
  1687. success: function(res) {
  1688. console.log("查看大图成功!");
  1689. },
  1690. error: function(res) {
  1691. that.errorMsg(res);
  1692. }
  1693. })
  1694. },
  1695. //复制文本
  1696. copyTxt: function(e) {
  1697. var that = this;
  1698. that.data.copyContentMsgid = e.currentTarget.dataset.id; //当前复制内容msgid
  1699. that.data.copyCotent = e.currentTarget.dataset.text; //当前复制内容
  1700. var query = wx.createSelectorQuery();
  1701. query.select('#copytext' + that.data.copyContentMsgid).boundingClientRect();
  1702. query.exec(function(res) {
  1703. that.data.copyButtonPosition = Number(res[0].width).toFixed(0) - 30;
  1704. that.setData({
  1705. copyButtonPosition: that.data.copyButtonPosition,
  1706. copyContentMsgid: that.data.copyContentMsgid,
  1707. })
  1708. })
  1709. },
  1710. //开始复制
  1711. startCopyContet: function() {
  1712. var that = this;
  1713. wx.setClipboardData({
  1714. data: that.data.copyCotent,
  1715. success: function(res) {
  1716. // wx.hideToast();
  1717. console.log("复制成功")
  1718. }
  1719. })
  1720. that.setData({
  1721. copyContentMsgid: "",
  1722. })
  1723. },
  1724. //版本号比较
  1725. compareVersion: function(v1, v2) {
  1726. v1 = v1.split('.')
  1727. v2 = v2.split('.')
  1728. const len = Math.max(v1.length, v2.length)
  1729. while (v1.length < len) {
  1730. v1.push('0')
  1731. }
  1732. while (v2.length < len) {
  1733. v2.push('0')
  1734. }
  1735. for (let i = 0; i < len; i++) {
  1736. const num1 = parseInt(v1[i])
  1737. const num2 = parseInt(v2[i])
  1738. if (num1 > num2) {
  1739. return 1
  1740. } else if (num1 < num2) {
  1741. return -1
  1742. }
  1743. }
  1744. return 0
  1745. },
  1746. //用户点击右上角分享
  1747. onSharethatMessage: function(res) {
  1748. var that = this;
  1749. var title = that.data.tiny.length == 0 ? "忠仕微客宝" : that.data.tiny;
  1750. if (res.from === 'button') {} //判断哪种方式分享,menu右上角分享,button 页面内分享
  1751. return {
  1752. title: title,
  1753. path: '/pages/tripartite/tripartite?from_openid=' + that.globalUserData.openid +
  1754. '&from_siteid=' + that.globalUserData.siteid,
  1755. }
  1756. },
  1757. //复制客服人员信息(微信)
  1758. copyUserInfor: function(e) {
  1759. var that = this;
  1760. //button 获取formid
  1761. if (e.detail.formId == 'the formId is a mock one' || e.detail.formId == undefined) {} else {
  1762. // var item = {
  1763. // formid: e.detail.formId,
  1764. // formidcreatetime: util.formatTime(new Date())
  1765. // }
  1766. // that.data.formidArray.push(item);
  1767. that.data.formid = e.detail.formId;
  1768. that.data.formidcreatetime = util.formatTime(new Date());
  1769. that.formidUpload();
  1770. }
  1771. var cooytext = that.data.cusWechat;
  1772. if (cooytext.length == 0) {
  1773. that.errorMsg("该客服没有设置微信号!");
  1774. return;
  1775. }
  1776. wx.setClipboardData({
  1777. data: cooytext,
  1778. success: function(res) {
  1779. wx.hideToast();
  1780. that.setData({
  1781. addfriendguide: "block"
  1782. })
  1783. //that.data.msgcontent="微信号复制成功:请返回微信--添加朋友--粘贴即可添加客服微信"
  1784. // that.sendMsgPara("0", "2", "微信号复制成功:请返回微信--添加朋友--粘贴即可添加客服微信");
  1785. },
  1786. fail: function(res) {
  1787. that.errorMsg(res.errMsg);
  1788. }
  1789. })
  1790. },
  1791. //访客发送消息(参数带msgcontent)
  1792. sendMsgPara: function(msgtype, msgsource, msgcontent) {
  1793. var that = this;
  1794. var msgid = that.createCode(32); //生成msgid 32位字符串
  1795. var nowtime = util.formatTime(new Date());
  1796. //向直接向页面赋值
  1797. var item = {
  1798. viewContent: that.splitMsgcontent(msgcontent),
  1799. content: msgcontent,
  1800. msgid: msgid,
  1801. msgtype: msgtype,
  1802. source: msgsource,
  1803. tick: "99999999999999999", //生成最大tick 保证该消息在最后一条
  1804. time: nowtime.substring(11, nowtime.length),
  1805. timedifference: "",
  1806. voicewidth: "",
  1807. msgsendstate: "0", //信息发送状态 0 正在发送
  1808. };
  1809. that.data.msg.push(item);
  1810. that.setData({
  1811. msgArray: that.data.msg,
  1812. // msgcontent: "",
  1813. // scrollTop: 2147483647 + "px",
  1814. // textareacursor: 0,
  1815. // btnSendOrAddMenu: 1,
  1816. funhight: "55rpx",
  1817. textareahight: "48rpx",
  1818. menushow: "none",
  1819. isshowemoji: "none",
  1820. menuheight: "110rpx",
  1821. });
  1822. that.scrollToTheBottom();
  1823. // that.setData({
  1824. // scrollTop: 2147483647 + "px",
  1825. // });
  1826. wx.request({
  1827. url: that.globalUserData.serviceurl + "/api/UserDockingService.ashx?action=send",
  1828. // url:"http://localhost:62825/api/Chat.ashx?action=send",
  1829. header: {
  1830. 'content-type': 'thatlication/json' // 默认值
  1831. },
  1832. data: {
  1833. siteid: that.siteid,
  1834. sid: that.data.nowtalksid,
  1835. msg: msgcontent,
  1836. source: msgsource,
  1837. msgtype: msgtype,
  1838. // formid: "",
  1839. // formidcreatetime: "",
  1840. msgid: msgid,
  1841. },
  1842. method: "POST",
  1843. success: function(e) {
  1844. //成功回调
  1845. if (e.data.success) {} else {
  1846. if (e.data.error == "no chat" && that.isAtTalkPage) {
  1847. that.openChat();
  1848. console.log("已重新开启对话!");
  1849. } else {
  1850. console.log(e.data.error);
  1851. return false;
  1852. }
  1853. }
  1854. },
  1855. error: function(e) {
  1856. console.log("发送消息失败" + e.data.error);
  1857. return
  1858. },
  1859. complete: function() {},
  1860. });
  1861. },
  1862. //数组移除消息
  1863. arrarRemove: function(array, msgid) {
  1864. for (var i = 0; i < array.length; i++) {
  1865. if (array[i].msgid == msgid) {
  1866. array.splice(i, 1);
  1867. }
  1868. }
  1869. return array;
  1870. },
  1871. //消息滚动顶部事件
  1872. scrollToupper: function(e) {
  1873. var that = this;
  1874. //阻止事件多次触发
  1875. if (!that.data.ifscrollToupper) {
  1876. return;
  1877. }
  1878. //是否存在下拉数据
  1879. if (that.data.loadingcontent == "没有更多了") {
  1880. setTimeout(function() {
  1881. that.setData({
  1882. ifloading: true,
  1883. });
  1884. }, 1000)
  1885. return;
  1886. }
  1887. //获取当前页面消息最小的tick值
  1888. var mintick = that.data.msg[0].tick;
  1889. for (var i = 0; i < that.data.msg.length; i++) {
  1890. if (that.data.msg[i].tick < mintick) {
  1891. mintick = that.data.msg[i].tick
  1892. }
  1893. }
  1894. //加载数据显示
  1895. that.setData({
  1896. ifloading: false,
  1897. loadingcontent: "正在加载..."
  1898. });
  1899. setTimeout(function() {
  1900. wx.request({
  1901. url: that.globalUserData.serviceurl +
  1902. "/api/Chat.ashx?action=getmsgbycidandtick",
  1903. //url: "http://localhost:62825/api/Chat.ashx?action=getmsgbycidandtick",
  1904. header: {
  1905. 'content-type': 'thatlication/json' // 默认值
  1906. },
  1907. data: {
  1908. siteid: that.siteid,
  1909. cid: "LRWT_" + that.globalUserData.openid,
  1910. tick: mintick,
  1911. size: "10"
  1912. },
  1913. method: "POST",
  1914. success: function(e) {
  1915. if (e.data.success) {
  1916. if (e.data.list.length != 0) {
  1917. for (var i = 0; i < e.data.list.length; i++) {
  1918. var item = {};
  1919. if (e.data.list[i].msgtype == "0") {
  1920. var viewContent = that.splitMsgcontent(e.data.list[
  1921. i].content);
  1922. item = {
  1923. viewContent: viewContent,
  1924. content: e.data.list[i].content,
  1925. msgid: e.data.list[i].msgid,
  1926. msgtype: e.data.list[i].msgtype,
  1927. source: e.data.list[i].source,
  1928. tick: e.data.list[i].tick,
  1929. time: e.data.list[i].time,
  1930. }
  1931. that.data.msg.push(item);
  1932. } else if (e.data.list[i].msgtype == "1") {
  1933. var newcontent = that.replacecontent(e.data.list[i]
  1934. .content, /\\/g, "/");
  1935. item = {
  1936. content: newcontent,
  1937. msgid: e.data.list[i].msgid,
  1938. msgtype: e.data.list[i].msgtype,
  1939. source: e.data.list[i].source,
  1940. tick: e.data.list[i].tick,
  1941. time: e.data.list[i].time,
  1942. };
  1943. that.data.msg.push(item);
  1944. } else {
  1945. that.data.msg.push(e.data.list[i]);
  1946. }
  1947. }
  1948. that.setData({
  1949. msgArray: that.data.msg.sort(that.compare(
  1950. "tick")),
  1951. });
  1952. } else {
  1953. that.setData({
  1954. loadingcontent: "没有更多"
  1955. });
  1956. }
  1957. // that.setData({
  1958. // ifloading: true,
  1959. // scrollTop: 10
  1960. // });
  1961. setTimeout(function() {
  1962. that.setData({
  1963. ifloading: true,
  1964. scrollTop: 10
  1965. });
  1966. }, 500)
  1967. } else {
  1968. that.setData({
  1969. loadingcontent: "下拉获取历史消息失败",
  1970. }, setTimeout(function() {
  1971. that.setData({
  1972. ifloading: true,
  1973. scrollTop: 10,
  1974. })
  1975. }, 500));
  1976. }
  1977. },
  1978. error: function(res) {
  1979. wx.showLoading({
  1980. title: '下拉获取历史消息失败',
  1981. })
  1982. that.setData({
  1983. ifloading: true,
  1984. scrollTop: 10,
  1985. });
  1986. console.log(res.error);
  1987. },
  1988. complete: function() {
  1989. that.setData({
  1990. ifscrollToupper: true
  1991. });
  1992. }
  1993. })
  1994. }, 2000)
  1995. that.setData({
  1996. ifscrollToupper: false
  1997. });
  1998. },
  1999. //滚动条滚动底部方法
  2000. scrollToTheBottom: function() {
  2001. var that = this;
  2002. setTimeout(function() {
  2003. that.setData({
  2004. scrollTop: 2147483647 + "px",
  2005. })
  2006. }, 200)
  2007. },
  2008. //拆分文字,图片
  2009. splitMsgcontent: function(msgcontent) {
  2010. var that = this;
  2011. var textAndEmojiJson = [];
  2012. var strTextAndEmojiList = [];
  2013. var emojilist = that.data.wxemojliList;
  2014. var newMsgContent = msgcontent;
  2015. for (var i = 0; i < emojilist.length; i++) {
  2016. if (msgcontent.indexOf(emojilist[i].wxemojiid) != -1) {
  2017. newMsgContent = newMsgContent.replace(new RegExp(emojilist[i].wxemojiid, 'g'), emojilist[i]
  2018. .wxemojimarhing);
  2019. //newMsgContent = that.data.msgcontent.replaceAll(emojilist[i].wxemojiid, emojilist[i].wxemojimarhing);
  2020. }
  2021. }
  2022. strTextAndEmojiList = newMsgContent.split("_&");
  2023. //去除数组为空的元素
  2024. if (strTextAndEmojiList.length != 0) {
  2025. for (var j = 0; j < strTextAndEmojiList.length; j++) {
  2026. if (strTextAndEmojiList[j] == "") {
  2027. strTextAndEmojiList.splice(j, 1);
  2028. j = j - 1;
  2029. } else {
  2030. var item = {};
  2031. if (strTextAndEmojiList[j].indexOf(".gif-/a") != -1) {
  2032. item = {
  2033. bisemoji: 0, //0 表情 1文字
  2034. content: strTextAndEmojiList[j].substring(0, strTextAndEmojiList[j].length -
  2035. 3)
  2036. }
  2037. } else {
  2038. item = {
  2039. bisemoji: 1, //0 表情 1文字
  2040. content: strTextAndEmojiList[j]
  2041. }
  2042. }
  2043. textAndEmojiJson.push(item);
  2044. }
  2045. }
  2046. }
  2047. // that.setData({
  2048. // msgNewContent: textAndEmojiJson
  2049. // })
  2050. return textAndEmojiJson;
  2051. },
  2052. //发送当位置
  2053. choosePotsion: function() {
  2054. var that = this;
  2055. wx.chooseLocation({
  2056. success: function(res) {
  2057. console.log("1" + JSON.stringify(res));
  2058. var name = res.name;
  2059. var address = res.address;
  2060. var latitude = res.latitude;
  2061. var longitude = res.longitude;
  2062. var jsonposi = {
  2063. name: name,
  2064. address: address,
  2065. latitude: latitude,
  2066. longitude: longitude,
  2067. };
  2068. that.data.msgcontent = jsonposi;
  2069. that.sendMsg(3, 0); //%%%
  2070. },
  2071. fail: function() {
  2072. wx.getSetting({
  2073. success(res) {
  2074. if (!res.authSetting['scope.userLocation']) {
  2075. // 禁止获取地理位置后重新调用授权后,再次获取
  2076. console.log("2" + res)
  2077. wx.authorize({
  2078. scope: 'scope.userLocation',
  2079. success() {
  2080. wx.chooseLocation({
  2081. success: function(res) {
  2082. // 禁止获取地理位置后重新调用授权后,获取位置成功
  2083. console.log("3" + res)
  2084. },
  2085. })
  2086. },
  2087. fail() {
  2088. // 禁止获取地理位置后重新调用授权后,获取位置失败
  2089. console.log("f2")
  2090. }
  2091. })
  2092. }
  2093. }
  2094. })
  2095. }
  2096. })
  2097. },
  2098. //在地图展示位置
  2099. getLocation: function(event) {
  2100. var that = this;
  2101. console.log(event.currentTarget.dataset.gid);
  2102. if (event.currentTarget.dataset.gid == "") {
  2103. return;
  2104. }
  2105. that.loadingshow();
  2106. //调用地址解析接口
  2107. qqmapsdk.geocoder({
  2108. //获取表单传入地址
  2109. address: event.currentTarget.dataset.gid, //地址参数,例:固定地址,address: '北京市海淀区彩和坊路海淀西大街74号'
  2110. success: function(res) { //成功后的回调
  2111. that.loadinghide();
  2112. console.log(res);
  2113. var res = res.result;
  2114. wx.openLocation({
  2115. longitude: res.location.lng,
  2116. latitude: res.location.lat,
  2117. name: event.currentTarget.dataset.gid,
  2118. })
  2119. },
  2120. fail: function(error) {
  2121. console.error(error);
  2122. that.errorMsg("打开地图失败!")
  2123. },
  2124. complete: function(res) {
  2125. console.log(res);
  2126. }
  2127. })
  2128. },
  2129. //生成随机数
  2130. createCode: function(length) {
  2131. var code;
  2132. //首先默认code为空字符串
  2133. code = '';
  2134. //设置长度,这里看需求,我这里设置了
  2135. var codeLength = length;
  2136. //设置随机字符
  2137. var random = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
  2138. //循环codeLength 我设置的4就是循环4次
  2139. for (var i = 0; i < codeLength; i++) {
  2140. //设置随机数范围,这设置为0 ~ 36
  2141. var index = Math.floor(Math.random() * 9);
  2142. //字符串拼接 将每次随机的字符 进行拼接
  2143. code += random[index];
  2144. }
  2145. return code;
  2146. },
  2147. /**
  2148. * 正确消息
  2149. */
  2150. showMsg: function(title) {
  2151. wx.showToast({
  2152. title: title,
  2153. image: "/images/duihao.png"
  2154. });
  2155. },
  2156. /**
  2157. * 错误消息
  2158. */
  2159. errorMsg: function(title) {
  2160. wx.showToast({
  2161. title: title,
  2162. image: "/images/error.png"
  2163. });
  2164. },
  2165. loadingshow: function() {
  2166. wx.showLoading({
  2167. title: '加载中',
  2168. })
  2169. },
  2170. loadinghide: function() {
  2171. setTimeout(function() {
  2172. wx.hideLoading()
  2173. }, 0)
  2174. },
  2175. replacecontent: function(originalcontent, character, tocharacter) {
  2176. var content = "";
  2177. content = originalcontent.replace(character, tocharacter);
  2178. return content;
  2179. },
  2180. initInterval: function() {
  2181. var das;
  2182. },
  2183. //是否正常获取站点,以及正常开启对话
  2184. checkConnectOk() {
  2185. var conncetmsg = "";
  2186. if (!this.serverconncet) {
  2187. conncetmsg = "开启对话失败!"
  2188. }
  2189. return conncetmsg;
  2190. },
  2191. })