酷店app

index.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. var classNumber = 0,token = '',userImg = 'http://imgs.726p.com/tbk_onlive/default-img/kx-5ca5de6595506.jpeg',setLocalStorage = '';
  2. $().ready(function(){
  3. document.body.addEventListener('focusout', function () {
  4. window.scrollTo(0,0);
  5. scrollToEnd();
  6. });
  7. //点击关闭图
  8. $("#dialog_large_image").click(function () {
  9. $("#dialog_large_image").fadeOut(200);
  10. $("#dialog_large_image").html('')
  11. })
  12. getToken();//获取token
  13. getLocalStorage();//获取本地信息存储的信息
  14. document.getElementsByClassName("problemButton")[0].addEventListener('touchstart', submitFeedback, false);
  15. })
  16. //获取token
  17. function getToken () {
  18. var param = '{"js_callback" : "callBackMethodName"}';
  19. try{
  20. if(navigator.userAgent.match(/(iPhone|iPod|iPad);?/i)){
  21. window.webkit.messageHandlers.app_h5_get_token.postMessage(param);
  22. }
  23. if(navigator.userAgent.match(/android/i)){
  24. obj4H5.app_h5_get_token(param);
  25. }
  26. }catch(e){
  27. //TODO handle the exception
  28. // token='51fb5c632983ff2c21f057d6759db61f29'
  29. // userinfo();
  30. }
  31. }
  32. function callBackMethodName (data) {//app token 回调
  33. var data = JSON.parse(data)
  34. token = data.token;
  35. // showMsg(token)
  36. userinfo();
  37. }
  38. //保存数据到本地
  39. function appH5SaveMessage () {
  40. var param = '{"uploadData" : ' + setLocalStorage + '}';
  41. try{
  42. if(navigator.userAgent.match(/(iPhone|iPod|iPad);?/i)){
  43. window.webkit.messageHandlers.app_h5_save_message.postMessage(param);
  44. }
  45. }catch(e){
  46. //TODO handle the exception
  47. }
  48. }
  49. //获取本地信息
  50. function getLocalStorage () {
  51. var param = '{"js_callback" : "getLocalStorageIos"}';
  52. try {
  53. if(navigator.userAgent.match(/(iPhone|iPod|iPad);?/i)){
  54. window.webkit.messageHandlers.app_h5_get_message.postMessage(param);
  55. }else{
  56. getLocalOperation('android',localStorage.getItem("last_feedback"));
  57. }
  58. } catch (error) {
  59. getLocalOperation('android',localStorage.getItem("last_feedback"));
  60. }
  61. }
  62. function getLocalStorageIos (data) {
  63. getLocalOperation('ios',data);
  64. }
  65. function getLocalOperation (type,data){
  66. //获取到本地后的操作
  67. var localStorages = JSON.parse(data);
  68. if(type == 'ios'){
  69. localStorages = localStorages.uploadData;
  70. }else{
  71. if(!localStorage.getItem("last_feedback")){
  72. return;
  73. }
  74. }
  75. if(localStorages.img){
  76. $(".serviceContent").append('<li class="userFeedback"><div class="serviceConDivUser"><img class="feedbackImg" src="' + localStorages.img + '"/></div><img class="headPortrait" id="headPortrait" src="' + userImg + '"/></li>')
  77. }else{
  78. $(".serviceContent").append(' <li class="userFeedback"><div class="serviceConDiv serviceConDivUser"><h3>这是我要反馈的问题</h3><p>' + localStorages.feedbackCon + '</p></div><img class="headPortrait" id="headPortrait" src="' + userImg + '"/></li>')
  79. }
  80. reply();
  81. $(".serviceContent").append('<li class="coolStore"><div class="serviceHint">只显示最新一条反馈,其他反馈已在处理</div></li>')
  82. scrollToEnd();
  83. lookBigImg('null');//点击查看大图
  84. }
  85. function lookBigImg(html){
  86. //点击查看大图
  87. var $html = '';
  88. if(html != 'null'){
  89. $html = "#" + html + " .feedbackImg";
  90. }else{
  91. $html = ".feedbackImg"
  92. }
  93. $($html).click(function () {
  94. $("#dialog_large_image").fadeIn(200);
  95. $("#dialog_large_image").html('<img src="' + $(this).attr('src') + '"/>')
  96. })
  97. }
  98. function changeImgID() {
  99. //获取读取我文件的File对象
  100. var selectedFile = '';
  101. selectedFile = document.getElementById('imgInput').files[0];
  102. var reader = new FileReader();
  103. reader.readAsDataURL(selectedFile);
  104. reader.onload = function () {
  105. //当读取完成后回调这个函数,然后此时文件的内容存储到了result中,直接操作即可
  106. console.log(this)
  107. //图片压缩
  108. canvasDataURL(this.result,{
  109. quality: 0.3
  110. },function (base64Codes) {
  111. // console.log(base64Codes)
  112. classNumber += 1;
  113. $(".serviceContent").append('<li class="userFeedback" id="uploadLoading_' + classNumber + '"><div class="serviceConDivUser"><img class="uploadLoading" src="../common/loading.gif"/><img class="feedbackImg" src="' + base64Codes + '"/></div><img class="headPortrait" src="' + userImg + '"/></li>')
  114. scrollToEnd();
  115. var json = {
  116. loadingId : 'uploadLoading_' + classNumber,
  117. img : base64Codes
  118. }
  119. uploadFeedbackEvent(JSON.stringify(json))
  120. lookBigImg('uploadLoading_' + classNumber);
  121. })
  122. }
  123. }
  124. function submitFeedback () {
  125. //点击提交反馈按钮
  126. var feedback_con = $("#problemInput").val().replace(/(^\s*)|(\s*$)/g, "");
  127. if(feedback_con == ''){
  128. showMsg('请输入您遇到的问题')
  129. return;
  130. }
  131. classNumber += 1;
  132. $(".serviceContent").append(' <li class="userFeedback" id="uploadLoading_' + classNumber + '"><div class="serviceConDiv serviceConDivUser"><img class="uploadLoading" src="../common/loading.gif"/><h3>这是我要反馈的问题</h3><p>' + feedback_con + '</p></div><img class="headPortrait" src="' + userImg + '"/></li>')
  133. $("#problemInput").val('')
  134. scrollToEnd();
  135. $(".serviceButtom #problemInput").blur();
  136. var json = {
  137. loadingId : 'uploadLoading_' + classNumber,
  138. feedbackCon : feedback_con
  139. }
  140. uploadFeedbackEvent(JSON.stringify(json))
  141. }
  142. function uploadFeedbackEvent(data){
  143. //提交反馈事件
  144. var uploadData = JSON.parse(data)
  145. // console.log(uploadData)
  146. var ajaxData = null;
  147. if(uploadData.img){//上传图片
  148. ajaxData = {
  149. 'type':1,
  150. 'content':'',
  151. 'img':uploadData.img
  152. }
  153. upUserComplainH5(JSON.stringify(ajaxData),data)
  154. }else{//上传文案
  155. ajaxData = {
  156. 'type':0,
  157. 'content':uploadData.feedbackCon,
  158. 'img':''
  159. }
  160. upUserComplainH5(JSON.stringify(ajaxData),data)
  161. }
  162. setTimeout(function () {
  163. },2000)
  164. }
  165. function upUserComplainH5 (dataes,uploadData) {
  166. //h5客服记录
  167. $.ajax({
  168. url:'/api/userComplain/upUserComplainH5',
  169. type:'post',
  170. dataType:'json',
  171. headers:{
  172. 'token': token
  173. },
  174. data: JSON.parse(dataes),
  175. success:function (res){
  176. console.log(res)
  177. if(res.errno == 0){
  178. $("#" + JSON.parse(uploadData).loadingId + " .uploadLoading").hide();
  179. setLocalStorage = uploadData;
  180. localStorage.setItem("last_feedback",uploadData); //存入
  181. appH5SaveMessage();//ios保存数据到本地
  182. reply();
  183. }else if(res.errno == 4002){
  184. showMsg(res.err)
  185. $("#" + JSON.parse(uploadData).loadingId + " .uploadLoading").attr('src','./img/gantanhao.png');
  186. }else{
  187. $("#" + JSON.parse(uploadData).loadingId + " .uploadLoading").attr('src','./img/gantanhao.png');
  188. }
  189. },
  190. error:function (err){
  191. $("#" + JSON.parse(uploadData).loadingId + " .uploadLoading").attr('src','./img/gantanhao.png');
  192. }
  193. })
  194. }
  195. function userinfo () {
  196. //获取用户信息
  197. $.ajax({
  198. url:'/api/users/userinfo',
  199. type:'post',
  200. dataType:'json',
  201. headers:{
  202. 'token': token
  203. },
  204. success:function (res){
  205. console.log(res)
  206. if(res.errno == 0){
  207. userImg = res.rst.data.img;
  208. $("#headPortrait").attr('src',userImg)
  209. }else if(res.errno == 4002){
  210. showMsg(res.err)
  211. }
  212. },
  213. error:function (err){
  214. $("#" + JSON.parse(uploadData).loadingId + " .uploadLoading").attr('src','./img/gantanhao.png');
  215. }
  216. })
  217. }
  218. function scrollToEnd(){//滚动到底部
  219. $(document).scrollTop(0);
  220. $(".serviceContent").scrollTop($(".serviceContent")[0].scrollHeight + 105);
  221. }
  222. function reply () {
  223. //统一回复信息
  224. // $(".serviceContent").append('<li class="coolStore"><img class="headPortrait" src="./img/service.png"/><div class="serviceConDiv"><h3>您的反馈我们已经收到</h3><p>正在为您火速解决问题~<br/>您也可以添加客服微信咨询<br/>客服微信:wufazhucexiaohai<button onclick="copyEvent()">复制微信号</button></p></div></li>')
  225. $(".serviceContent").append('<li class="coolStore"><img class="headPortrait" src="./img/service.png"/><div class="serviceConDiv"><h3>您的反馈我们已经收到</h3><p>正在为您火速解决问题~</p></div></li>')
  226. scrollToEnd();
  227. }
  228. function goDetailGuide (origin) {
  229. //去二级页面
  230. window.location.href = './detail_guide.html?origin=' + origin
  231. }
  232. //图片压缩
  233. function canvasDataURL(path, obj, callback){
  234. var img = new Image();
  235. img.src = path;
  236. img.onload = function(){
  237. var that = this;
  238. // 默认按比例压缩
  239. var w = that.width,
  240. h = that.height,
  241. scale = w / h;
  242. w = obj.width || w;
  243. h = obj.height || (w / scale);
  244. var quality = 0.4; // 默认图片质量为0.7
  245. //生成canvas
  246. var canvas = document.createElement('canvas');
  247. var ctx = canvas.getContext('2d');
  248. // 创建属性节点
  249. var anw = document.createAttribute("width");
  250. anw.nodeValue = w;
  251. var anh = document.createAttribute("height");
  252. anh.nodeValue = h;
  253. canvas.setAttributeNode(anw);
  254. canvas.setAttributeNode(anh);
  255. ctx.drawImage(that, 0, 0, w, h);
  256. // 图像质量
  257. if(obj.quality && obj.quality <= 1 && obj.quality > 0){
  258. quality = obj.quality;
  259. }
  260. // quality值越小,所绘制出的图像越模糊
  261. var base64 = canvas.toDataURL('image/jpeg', quality);
  262. // 回调函数返回base64的值
  263. callback(base64);
  264. }
  265. }
  266. function copyEvent () {
  267. // var data=$("#wechatNumber").html();
  268. var data = 'wufazhucexiaohai';
  269. copy_2.innerHTML = data;
  270. copy_1.value = data;
  271. if (navigator.userAgent.match(/(iPhone|iPod|iPad);?/i)) {
  272. //区分iPhone设备
  273. window.getSelection().removeAllRanges();//这段代码必须放在前面否则无效
  274. var Url2=document.getElementById("copy_2");//要复制文字的节点
  275. var range = document.createRange();
  276. // 选中需要复制的节点
  277. range.selectNode(Url2);
  278. // 执行选中元素
  279. window.getSelection().addRange(range);
  280. // 执行 copy 操作
  281. var successful = document.execCommand('copy');
  282. // 移除选中的元素
  283. window.getSelection().removeAllRanges();
  284. }else{
  285. var Url2=document.getElementById("copy_1");//要复制文字的节点
  286. Url2.select(); // 选择对象
  287. document.execCommand("Copy"); // 执行浏览器复制命令
  288. }
  289. showMsg('复制成功')
  290. }
  291. //提示弹框
  292. function showMsg(msg) {
  293. var msgBox = document.getElementsByClassName('alert-info')[0];
  294. msgBox.children[0].innerText = msg;
  295. msgBox.style.display="block";
  296. setTimeout(function() {
  297. msgBox.style.display="none";
  298. }, 1500);
  299. }