Browse Source

计量单位转换

pull/18/head
pengda 7 months ago
parent
commit
880bfd2b98
  1. 5
      config/define.php
  2. 17
      control/index.php
  3. 9
      data/dFormula.php
  4. 28
      model/mFormula.php

5
config/define.php

@ -77,6 +77,11 @@
)
);
define('WEIGHT_LIANG', 1);
$GLOBALS['weight_convert_list'] = array(
'两'=> WEIGHT_LIANG,
);
$GLOBALS['weight_list'] = array(
"汉" => array(
'少许'=>'',

17
control/index.php

@ -331,18 +331,17 @@ class index extends publicBase {
public function ajax_unit_conv() {
$uinfo = $this->_check_login();
$name = trim($this->post('name')) ? trim($this->post('name')) : '两';
$unit_name = '两';
$unit_type = $GLOBALS['weight_convert_list'][$unit_name];
$m_formula = new mFormula();
$unit = $m_formula->getUnitByName($name);
$unit_conv = $m_formula->getUnitConvList($unit_type);
$unit_conv = $m_formula->getUnitConvList($unit['id']);
$user_conv = $m_formula->getUserConv($uinfo['uid'], $unit['id']);
$user_conv = $m_formula->getUserConv($uinfo['uid'], $unit_type);
foreach ($unit_conv as &$conv) {
$conv['conv_txt'] = "1{$unit['name']}={$conv['num']}g";
$conv['conv_txt'] = "1{$unit_name}={$conv['num']}g";
if($user_conv['conv_id'] == $conv['id']){
$user_conv['conv_txt'] = "当前1{$unit['name']}={$conv['num']}g";
$user_conv['conv_txt'] = "当前1{$unit_name}={$conv['num']}g";
}
}
@ -352,11 +351,11 @@ class index extends publicBase {
public function ajax_set_user_conv() {
$uinfo = $this->_check_login();
$unit_id = trim($this->post('unit_id')) + 0;
$unit_type = trim($this->post('unit_type')) + 0;
$conv_id = trim($this->post('conv_id')) + 0;
$m_formula = new mFormula();
$res = $m_formula->saveUserConv($uinfo['uid'], $unit_id, $conv_id);
$res = $m_formula->saveUserConv($uinfo['uid'], $unit_type, $conv_id);
if (!$res) $this->ajax_json(false, $m_formula->getError());
$this->ajax_json(true, '保存成功');

9
data/dFormula.php

@ -44,14 +44,10 @@ class dFormula extends dBase {
'formula_id',
'use_num',
),
'tcm_unit_list' => array(
'id',
'name',
),
'tcm_unit_conv' => array(
'id',
'uid',
'unit_id',
'unit_type',
'num',
'from',
'is_delete',
@ -60,7 +56,7 @@ class dFormula extends dBase {
'tcm_user_conv' => array(
'id',
'uid',
'unit_id',
'unit_type',
'conv_id',
),
);
@ -71,7 +67,6 @@ class dFormula extends dBase {
'tcm_herb' => 'id',
'tcm_collect_log' => 'id',
'tcm_formula_use_log' => 'id',
'tcm_unit_list' => 'id',
'tcm_unit_conv' => 'id',
'tcm_user_conv' => 'id',
);

28
model/mFormula.php

@ -12,7 +12,6 @@ class mFormula extends mBase {
private $herb_tbl;
private $collect_log_tbl;
private $formula_use_log_tbl;
private $unit_list;
private $unit_conv;
private $user_conv;
@ -23,7 +22,6 @@ class mFormula extends mBase {
$this->herb_tbl = 'tcm_herb';
$this->collect_log_tbl = 'tcm_collect_log';
$this->formula_use_log_tbl = 'tcm_formula_use_log';
$this->unit_list = 'tcm_unit_list';
$this->unit_conv = 'tcm_unit_conv';
$this->user_conv = 'tcm_user_conv';
}
@ -81,10 +79,10 @@ class mFormula extends mBase {
public function convByUserChoice($uid, $num_str) {
$unit_name = preg_replace('/\d/', '', $num_str);
$unit = $this->getUnitByName($unit_name);
if (!$unit) return false;
$unit_type = $GLOBALS['weight_convert_list'][$unit_name];
if (!$unit_type) return false;
$user_conv = $this->getUserConv($uid, $unit['id']);
$user_conv = $this->getUserConv($uid, $unit_type);
if (!$user_conv) return false;
$unit_conv = $this->getUnitConvById($user_conv['conv_id']);
@ -642,29 +640,25 @@ class mFormula extends mBase {
return $this->obj->count($this->collect_log_tbl);
}
public function getUnitByName($name) {
return $this->obj->select($this->unit_list, array('sql' => '`name`=?', 'vals' => array($name)));
}
public function getUnitConvById($id) {
return $this->obj->select($this->unit_conv, array('sql' => '`id`=? and `is_delete`=?', 'vals' => array($id, 0)));
}
public function getUnitConvList($unit_id) {
return $this->obj->selectAll($this->unit_conv, array('sql' => '`unit_id`=? and `is_delete`=?', 'vals' => array($unit_id, 0)));
public function getUnitConvList($unit_type) {
return $this->obj->selectAll($this->unit_conv, array('sql' => '`unit_type`=? and `is_delete`=?', 'vals' => array($unit_type, 0)));
}
public function getUserConv($uid, $unit_id) {
return $this->obj->select($this->user_conv, array('sql' => '`uid`=? and `unit_id`=?', 'vals' => array($uid, $unit_id)));
public function getUserConv($uid, $unit_type) {
return $this->obj->select($this->user_conv, array('sql' => '`uid`=? and `unit_type`=?', 'vals' => array($uid, $unit_type)));
}
public function saveUserConv($uid, $unit_id, $conv_id) {
if (!$unit_id || !$conv_id) {
public function saveUserConv($uid, $unit_type, $conv_id) {
if (!$unit_type || !$conv_id) {
$this->setError('参数错误');
return false;
}
$user_conv = $this->getUserConv($uid, $unit_id);
$user_conv = $this->getUserConv($uid, $unit_type);
if ($user_conv) {
$res = $this->updateUserConv($user_conv['id'], array('conv_id' => $conv_id));
if (!$res) {
@ -674,7 +668,7 @@ class mFormula extends mBase {
return true;
}
$res = $this->insertUserConv(array('uid' => $uid, 'unit_id' => $unit_id, 'conv_id' => $conv_id));
$res = $this->insertUserConv(array('uid' => $uid, 'unit_type' => $unit_type, 'conv_id' => $conv_id));
if (!$res) {
$this->setError('设置失败');
return false;

Loading…
Cancel
Save