|
| 1 | +module.exports = function (App) { |
| 2 | + App.prototype.checkVersion = function checkVersion(device_deploy_uuid, device_app_version, device_platform, cb) { |
| 3 | + //uuid=compare h5version,app_version= in(min,max) |
| 4 | + var compatible_binary = false; |
| 5 | + var update_available = false; |
| 6 | + var comp_result = false; |
| 7 | + //var uuid = ''; |
| 8 | + var zipurl = ''; |
| 9 | + //console.log(this.versions.find({ where: {device_platform:'versionType'}})) ; |
| 10 | + this.versions(function (err, results) { |
| 11 | + //console.log(results); |
| 12 | + for (i = 0; i < results.length; i++) { |
| 13 | + console.log('h5Version:' + results[i]['h5Version']); |
| 14 | + console.log('max:' + results[i]['binMax']); |
| 15 | + console.log('min:' + results[i]['binMin']); |
| 16 | + console.log('uuid:' + device_deploy_uuid + 'appver:' + device_app_version); |
| 17 | + var latest_h5 = results[i]['h5Version']; |
| 18 | + var obj = results[i]; |
| 19 | + var max = results[i]['binMax']; |
| 20 | + var min = results[i]['binMin']; |
| 21 | + var versionType = results[i]['versionType']; |
| 22 | + //todo:好像少了个判断 |
| 23 | + if (device_platform == versionType) { |
| 24 | + comp_result = compareH5(device_deploy_uuid, device_app_version, latest_h5, max, min, obj, cb); |
| 25 | + } else { |
| 26 | + cb(null, compatible_binary, update_available, {uuid: device_deploy_uuid, url: zipurl}); |
| 27 | + |
| 28 | + } |
| 29 | + |
| 30 | + } |
| 31 | + }); |
| 32 | + |
| 33 | + |
| 34 | + var compareH5 = function compareH5(device_deploy_uuid, device_app_version, latest_h5, max, min, obj, cb) { |
| 35 | + |
| 36 | + //if (parseInt(latest_h5) > parseInt(uuid)) { |
| 37 | + if (compare(latest_h5, device_deploy_uuid) === 1) { |
| 38 | + //if (parseInt(min) <= parseInt(app_version) && parseInt(app_version) <= parseInt(max)) { |
| 39 | + if (compare(device_app_version, min) === 1 && compare(max, device_app_version) === 1) { |
| 40 | + device_deploy_uuid = latest_h5; |
| 41 | + console.log(obj); |
| 42 | + zipurl = obj['url']; |
| 43 | + |
| 44 | + console.log('zurl:' + zipurl); |
| 45 | + compatible_binary = true; |
| 46 | + update_available = true; |
| 47 | + console.log('compatible_binary:' + compatible_binary); |
| 48 | + console.log('update_available:' + update_available); |
| 49 | + |
| 50 | + cb(null, compatible_binary, update_available, {uuid: latest_h5, url: zipurl}); |
| 51 | + |
| 52 | + return true; |
| 53 | + } else { |
| 54 | + device_deploy_uuid = latest_h5; |
| 55 | + compatible_binary = true; |
| 56 | + update_available = false; |
| 57 | + |
| 58 | + cb(null, compatible_binary, update_available, {uuid: device_deploy_uuid, url: zipurl}); |
| 59 | + |
| 60 | + return false; |
| 61 | + } |
| 62 | + } else { |
| 63 | + |
| 64 | + cb(null, compatible_binary, update_available, {uuid: device_deploy_uuid, url: zipurl}); |
| 65 | + return false; |
| 66 | + } |
| 67 | + |
| 68 | + }; |
| 69 | + |
| 70 | + |
| 71 | + }; |
| 72 | + |
| 73 | + |
| 74 | + var compare = function compare(v1, v2) { |
| 75 | + var v1_tmp = v1, v2_tmp = v2; |
| 76 | + v1 = v1.replace(/\./g, ''); |
| 77 | + v2 = v2.replace(/\./g, ''); |
| 78 | + console.log("去掉小数点:", v1, v2); |
| 79 | + var len = Math.max(v1.length, v2.length); |
| 80 | + v1 = v1 + Array(len - v1.length + 1).join(0); |
| 81 | + v2 = v2 + Array(len - v2.length + 1).join(0); |
| 82 | + console.log("补长对齐:", v1, v2); |
| 83 | + v1 = parseInt(v1.replace(/^0+/, '')); |
| 84 | + v2 = parseInt(v2.replace(/^0+/, '')); |
| 85 | + console.log("去掉前导0:", v1, v2); |
| 86 | + console.log("大的是:", Math.max(v1, v2)); |
| 87 | + var ret = v1 > v2 ? 1 : v1 == v2 ? 0 : -1; |
| 88 | + console.log("compare:", v1_tmp, " ? ", v2_tmp, " : ", ret); |
| 89 | + return ret; |
| 90 | + } |
| 91 | + |
| 92 | + |
| 93 | + App.remoteMethod('checkVersion', { |
| 94 | + isStatic: false, |
| 95 | + |
| 96 | + accepts: [ |
| 97 | + {arg: 'device_deploy_uuid', type: 'string'}, |
| 98 | + {arg: 'device_app_version', type: 'string'}, |
| 99 | + {arg: 'device_platform', type: 'string'} |
| 100 | + ], |
| 101 | + |
| 102 | + returns: [ |
| 103 | + {arg: 'compatible_binary', type: 'boolean'}, |
| 104 | + {arg: 'update_available', type: 'boolean'}, |
| 105 | + {arg: 'update', type: 'object'} |
| 106 | + ], |
| 107 | + |
| 108 | + http: {path: '/updates/check', verb: 'post'} |
| 109 | + }); |
| 110 | +}; |
0 commit comments