1 /*公共类*/
2 Ext.define('myUtil', {
3 statics: {
4 //store公用加载方法
5 storeLoadById: function (id) {
6 var store = Ext.getStore(id);
7 this.storeLoadByParams(store, null);
8 },
9 //store加载方法,带参数
10 storeLoadByParams: function (store, params) {
11 store.removeAll();
12 store.setProxy({
13 extraParams: params
14 });
15 this.storeLoad(store, true);
16 },
17 //加载stroe
18 storeLoad: function (store, isUp) {
19 var me = this;
20 if (store.getCount() < 1 || isUp) {
21 store.loadPage(1);
22 }
23 },
24 //list->info公用加载方法
25 showInfo: function (record, view, url, params, Summary) {
26 var me = this;
27 if (record.data.Summary) {
28 view.setData(record.data);
29 return;
30 }
31 Ext.Ajax.request({
32 url: url,
33 params: params,
34 success: function (result, request) {
35 result = Ext.decode(result.responseText);
36 record.set(result);
37 view.setData(record.data);
38 }
39 });
40 },
41 /*为Ext.Viewport添加一个消息提示组件*/
42 addMessage: function () {
43 Ext.Viewport.setMasked({
44 xtype: 'loadmask',
45 cls: 'message',
46 transparent: true,
47 indicator: false
48 });
49 this.hideMessage();
50 },
51 /*显示一个消息提示*/
52 showMessage: function (mes, autoHide) {
53 var me = this,
54 message = this.getMessage();
55 message.setMessage(mes);
56 message.show();
57 //是否自动关闭提示
58 if (autoHide) {
59 setTimeout(function () {
60 message.hide();
61 },
62 500);
63 }
64 },
65 /*隐藏消息提示*/
66 hideMessage: function () {
67 this.getMessage().hide();
68 },
69 //消息组件
70 getMessage: function () {
71 return Ext.Viewport.getMasked();
72 },
73 //向Viewport中push一个视图
74 ePop: function (xtype) {
75 var me = Ext.Viewport,
76 view = Ext.create(xtype);
77 //切换
78 me.animateActiveItem(view, {
79 type: 'slide',
80 direction: 'left'
81 });
82 },
83 //监控Viewport界面切换,切换时销毁旧视图
84 eActiveitemchange: function () {
85 var me = Ext.Viewport;
86 me.onAfter('activeitemchange',
87 function (t, value, oldValue, eOpts) {
88 if (oldValue) {
89 //强制销毁,防止销毁不完全引发错误
90 me.remove(oldValue, true);
91 }
92 });
93 },
94 //json数据转换成xml数据
95 iterateJson: function (json) {
96 var value = '',
97 arr = [];
98 for (var tag in json) {
99 value = json[tag];
100 if (Ext.isObject(value) || Ext.isArray(value)) {
101 value = this.iterateJson(value);
102 }
103 if (tag > -1) {
104 arr.push(value);
105 } else if (value != null && value != '') {
106 arr.push(this.format('<{0}>{1}</{0}>', tag, value));
107 }
108 }
109 return arr.join('');
110 },
111 //格式化字符串
112 format: function () {
113 return Ext.util.Format.format.apply(this, arguments);
114 },
115 //显示pick
116 showPick: function (xtype, params) {
117 var pick = Ext.create(xtype);
118 Ext.Viewport.add(pick);
119 pick.show(params);
120 },
121 //结束pick
122 endPick: function (xtype) {
123 var pick = Ext.Viewport.down(xtype);
124 if (pick) {
125 pick.endPick();
126 }
127 },
128 //重写ajax
129 overrideAjax: function () {
130 var me = this;
131 //开始加载
132 Ext.Ajax.on('beforerequest',
133 function (connection, options) {
134 if (!options.hidMessage) {
135 me.showMessage('正在努力加载中...');
136 }
137 });
138 //加载成功
139 Ext.Ajax.on('requestcomplete',
140 function (connection, options) {
141 me.hideMessage();
142 });
143 //加载失败
144 Ext.Ajax.on('requestexception',
145 function (connection, options) {
146 if (!options.hidMessage) {
147 me.showMessage('加载失败,请稍后再试...', true);
148 }
149 });
150 },
151 //重写Pick相关
152 overridePick: function () {
153 //汉化时间
154 Ext.apply(Ext.DateExtras.monthNames, ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"]);
155 //汉化DatePicker控件
156 Ext.define("Ext.zh.DatePicker", {
157 override: "Ext.picker.Date",
158 config: {
159 yearFrom: 2000,
160 monthText: '月',
161 dayText: '日',
162 yearText: '年'
163 }
164 });
165 //汉化Picker控件
166 Ext.define("Ext.local_zh_cn.Picker", {
167 override: "Ext.picker.Picker",
168 config: {
169 doneButton: '确定',
170 cancelButton: '取消'
171 }
172 });
173 },
174 //安卓返回按钮
175 pGback: function () {
176 var cardPanel = Ext.getCmp('cardPanel');
177 var back = cardPanel.getNavigationBar().backButtonStack;
178 if (back.length > 1) {
179 cardPanel.Back();
180 this.isExit = false;
181 } else {
182 if (this.isExit) {
183 navigator.app.exitApp();
184 } else {
185 this.isExit = true;
186 cardPanel.showMessage('再按一次退出程序');
187 }
188 }
189 },
190 //上传图片
191 openFileSelector: function () {
192 /*
193 *图片选择方式
194 *PHOTOLIBRARY:从相册中选取
195 *CAMERA:调用摄像头
196 *AVEDPHOTOALBUM:左右不明
197 */
198 var source = navigator.camera.PictureSourceType.PHOTOLIBRARY;
199 /*
200 *图片返回格式
201 *DATA_URL:64位字符串
202 *FILE_URI:返回文件路径
203 *NATIVE_URI:返回系统路径 iOS:eg. assets-library:// Android: content://
204 */
205 var destinationType = navigator.camera.DestinationType.FILE_URI;
206 /*
207 *媒体类型
208 *PICTURE:图片
209 *VIDEO:视频 始终返回FILE_URI格式
210 *ALLMEDIA:支持任意文件选择
211 */
212 var mediaType = navigator.camera.MediaType.PICTURE;
213 var options = {
214 quality: 50,
215 //图像质量[0-100]
216 destinationType: destinationType,
217 sourceType: source,
218 mediaType: mediaType
219 };
220 navigator.camera.getPicture(this.uploadFile, this.uploadBroken, options);
221 },
222 //图片选择失败
223 uploadBroken: function (message) {
224 this.showMessage(message, true);
225 },
226 //选择图片后上传
227 uploadFile: function (fileURI) {
228 var options = new FileUploadOptions();
229 options.fileKey = "userfile";
230 options.fileName = fileURI.substr(fileURI.lastIndexOf('/') + 1);
231 options.mimeType = "multipart/form-data";
232 options.chunkedMode = false;
233 ft = new FileTransfer();
234 var uploadUrl = encodeURI(config.imgUp);
235 this.showMessage('正在上传中,请等待...');
236 ft.upload(fileURI, uploadUrl, this.uploadSuccess, this.uploadFailed, options);
237 },
238 //文件上传成功
239 uploadSuccess: function (r) {
240 var res = Ext.decode(r.responseText);
241 this.showMessage(res.message, true);
242 },
243 //文件上传失败
244 uploadFailed: function (error) {
245 this.showMessage('图片上传失败...', true);
246 },
247 //app初始化执行
248 inIt: function () {
249 this.addMessage();
250 this.overrideAjax();
251 this.overridePick();
252 }
253 }
254 });